图表样式的美化

一、设置图表样式与映射表

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
x = np.arange(5)
y1 = [1200, 2400, 1800, 2200, 1600]
y2 = [1050, 2100, 1300, 1600, 1340]
bar_width = 0.6
tick_label = ["家庭", "小说", "心理", "科技", "儿童"]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x, y1, bar_width, color="b", align="center", label ="地区1")
ax.bar(x, y2, bar_width, bottom=y1, color="c", align="center", label="地区2")
ax.set_ylabel("采购数量(本)")
ax.set_xlabel("图书种类")
ax.set_title(" 地区1和地区2对各类图书的采购情况")
ax.grid(True, axis='y', color="gray", alpha=0.2)
ax.set_xticks(x)
ax.set_xticklabels(tick_label)
ax.legend()
plt.show()


在这里插入图片描述

二、选择线型

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
eurcny_2017 = np.array([6.8007, 6.8007, 6.8015, 6.8015, 6.8060,  6.8060, 6.8060, 6.8036, 
6.8025, 6.7877, 6.7835, 6.7758, 6.7700, 6.7463, 6.7519,6.7511, 
6.7511, 6.7539, 6.7265])
eurcny_2019 = np.array([6.8640, 6.8705, 6.8697, 6.8697, 6.8697,6.8881, 6.8853, 6.8856,
6.8677, 6.8662, 6.8662, 6.8662, 6.8827, 6.8761, 6.8635,6.8860, 
6.8737, 6.8796, 6.8841]) 
date_x = np.array([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18, 19, 24, 25, 26, 31])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(date_x, eurcny_2017, color='hotpink', linewidth=3,linestyle=':',label='2017年7月美元/人民币汇率')
ax.plot(date_x, eurcny_2019, color='#8a2e76', linestyle='--', linewidth=2, label='2019年7月美元/人民币汇率')
ax.set_title('2017年7月与2019年7月美元/人民币汇率走势')  
ax.set_xlabel('日期')
ax.set_ylabel('汇率')
ax.legend()
plt.show()

在这里插入图片描述

三、添加数据标记

(1)plot()函数与scatter()函数可以将标记的取值传递给marker参数

import matplotlib.pyplot as plt
plt.plot([1,2,3,4,5,6,7,8],[6,7,8,9,10,11,12,13],marker='X',markerfacecolor='y',
markersize=15,linestyle=':',color='#8a2e76')
plt.show()

```![在这里插入图片描述](https://img-blog.csdnimg.cn/633cc16fa6524ed0b1bd158879926308.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCY5bm055qE6YWS5LiN6aaZ,size_13,color_FFFFFF,t_70,g_se,x_16)

```go
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
sale_a = [2144, 4617, 7674, 6666]
sale_b = [853, 1214, 2414, 4409]
sale_c = [153, 155, 292, 680]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(sale_a, 'D-', sale_b, '^:', sale_c, 's--')
ax.grid(alpha=0.3)
ax.set_ylabel('销售额(万元)')
ax.set_xticks(np.arange(len(sale_c)))
ax.set_xticklabels(['第1季度','第2季度', '第3季度', '第4季度'])
ax.legend(['产品A','产品B','产品C'])
plt.show()

在这里插入图片描述

四、设置字体及注释文本属性添加

plt.plot([1, 2, 3], [3, 4, 5],linestyle='--')
plt.text(1.9, 3.75, 'y=x+2', bbox=dict(facecolor='y'), family='serif', fontsize=12, fontstyle='normal', rotation=0)
   

``![在这里插入图片描述](https://img-blog.csdnimg.cn/9df79a6257a44f3fa6f800928875c814.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bCY5bm055qE6YWS5LiN6aaZ,size_15,color_FFFFFF,t_70,g_se,x_16)
`

```go
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
x = np.arange(4, 19)
y_max =[32, 33, 34, 34, 33, 31, 30, 29, 30, 29, 26, 23, 21, 25,  31]
y_min = [19, 19, 20, 22, 22, 21, 22, 16, 18, 18, 17, 14, 15, 16 , 16]
plt.plot(x, y_max, marker='o', label='最高温度')
plt.plot(x, y_min, marker='o', label='最低温度')
x_temp = 4
for y_h, y_l in zip(y_max, y_min):
    plt.text(x_temp-0.3, y_h  + 0.7, y_h, family='SimHei', fontsize=8, fontstyle='normal')
    plt.text(x_temp-0.3, y_l  + 0.7, y_l, family='SimHei', fontsize=8, fontstyle='normal')
    x_temp  += 1
plt.title('未来15天最高气温和最低气温的走势')
plt.xlabel('日期')
plt.ylabel('温度($^\circ$C)')
plt.ylim(0, 40)
plt.legend()
plt.show()

在这里插入图片描述

五、填充区域

(1)使用fill()函数填充多边形
(2)使用fill_between()函数填充两条水平曲线之间的区域
(3)使用fill_betweenx()函数填充两条垂直曲线之间的区域

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 8 * np.pi, 1000)
sin_y = np.sin(x)
cos_y = np.cos(1.5 * x / np.pi) / 2
plt.plot(x, sin_y)
plt.plot(x, cos_y)
plt.fill_between(x, cos_y, sin_y, cos_y < sin_y, color='k', alpha=0.5)
plt.fill_between(x, cos_y, sin_y, cos_y > sin_y, color='orangered', alpha=0.5)
plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt
def koch_snowflake(order, scale=10):
    def _koch_snowflake_complex(order):
        if order == 0:
            # 初始三角形
            angles = np.array([0, 120, 240]) + 90
            return scale / np.sqrt(3) * np.exp(np.deg2rad(angles)  * 1j)
        else:
            ZR = 0.5 - 0.5j  * np.sqrt(3) / 3
            p1 = _koch_snowflake_complex(order - 1)  # 起点
            p2 = np.roll(p1, shift=-1)               # 终点
            dp = p2 - p1                             # 连接向量
            new_points = np.empty(len(p1) * 4, dtype=np.complex128)
            new_points[::4] = p1
            new_points[1::4] = p1 + dp / 3
            new_points[2::4] = p1 + dp * ZR
            new_points[3::4] = p1 + dp / 3 * 2
            return new_points
    points = _koch_snowflake_complex(order)
    x, y = points.real, points.imag
    return x, y
x, y = koch_snowflake(order=2)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.fill(x, y, facecolor='lightsalmon', edgecolor='orangered', linewidth=3)
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值