class Mat:
def __init__(self):
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置rc参数显示中文标题
plt.rcParams['axes.unicode_minus'] = False # 设置正常显示字符,用到这条语句是因为坐标轴包含负值,在调用上一条语句后不使用该语句则负号会乱码
self.plt = plt
# 曲线图
def plot_mat(self):
# plot
self.plt.figure(figsize=(12,6)) # 画布大小 (长度, 宽度)
# plt.plot(7,0) # x坐标 从0到7
# plt.plot(0, 6) # y坐标 从0到6
# plt.plot((0, 2, 6), (3, 7, 1)) # plt.plot(x, y) x,y 可以是list、tuple
# ylist = [i*i for i in range(10)]
# xlist = [j for j in range(10)]
# plt.xlim(min(xlist),max(xlist))
# plt.ylim(min(ylist), max(ylist))
# x坐标, 最低温度, 最高温度
xlist, min_tems, max_tems = func()
plt.plot(xlist,max_tems, 'r--', label='最高温度') #xlist x坐标数据, y坐标数据, r-- 红色虚线 label 图标名称
plt.plot(xlist, min_tems, 'b-', label='最低温度') #b-蓝色实线
plt.legend(loc='best', fontsize=12) # best :自动选择最佳位置。 fontsize: 图标长度
# best :自动选择最佳位置。
# upper right :将图例放在右上角。
# upper left :将图例放在左上角。
# lower left :将图例放在左下角。
# lower right :将图例放在右下角。
# right :将图例放在右边。
# center left :将图例放在左边居中的位置。
# center right :将图例放在右边居中的位置。
# lower center :将图例放在底部居中的位置。
# upper center :将图例放在顶部居中的位置。
# center :将图例放在中心。
plt.xlabel('日期')
plt.ylabel('温度')
plt.title('温度趋势图')
plt.show()
# 指定点注释图
def specify_point_comments(self):
# annotate
x = np.linspace(0, 10, 200) # 从0到10之间等距产生200个值
y = np.sin(x)
# linestyle:{"solid": '-', "dotted": ":", "dashed": '--', "dashdot": "-."} linestyle=':'或者'dotted'都可以
plt.plot(x, y, linestyle='dashdot', color='b')
# 添加注释
# text: 注释信息内容
# xy:箭头点所在的坐标位置
# xytext:注释内容的坐标位置
# color:文字颜色
# arrowprops:设置指向箭头的参数, {arrowstyle:线条样式, color:线条颜色, lw:粗细, widthA: 箭头指向点的|长度,widthB: 箭头另一端|的长度}
# arrowstyle 有 '-|>', '|-|',写英文或符号都可以 图:桌面/arrowstyle
# 箭头长度可有 xytext 坐标控制
plt.annotate(text='标记点', xy=(3, np.sin(3)), xytext=(4, -0.5), weight='bold', color='b',arrowprops={'arrowstyle': '-|>', 'color': 'k'})
plt.annotate(text='aa', xy=(8, np.sin(8)), xytext=(8, 0.50), weight='bold', color='b',arrowprops={'arrowstyle': '-|>', 'color': 'r','lw':3})
plt.annotate(text='bb',xy=(4.5,np.sin(4.5)),xytext=(4.5,-0.50),arrowprops=dict(arrowstyle='|-|, widthA=0.1, widthB=0.5', lw=2))
plt.show()
# 子图
def sub_mat(self):
# subplot
x = np.linspace(0,15,300)
p1 = plt.subplot(2, 2, 1) # 2行2列 第1张图
plt.plot(x,np.sin(x), 'r')
plt.subplot(2, 2, 2, sharey=p1) # 与 p1 共享y轴
plt.plot(x, np.cos(x), 'b')
p3 = plt.subplot(2, 2, 3)
plt.plot(x,x, 'g')
plt.subplot(2, 2, 4, sharey=p3) # sharey=ax3与 ax3 共享y轴
plt.plot(x, 2*x, 'y')
plt.show()
# 柱状图
def bar_mat(self):
# bar
y = np.arange(10, 90, 20)
x = np.array(['张三', '李四', '王五', '钱三一'])
self.plt.bar(x, y, width=0.1, bottom=10)
self.plt.title('成绩单')
self.plt.show()
self.plt.barh(x, y)
self.plt.show()
# 散点图
def scatter_mat(self):
# scatter
x = np.linspace(0, 10, 10)
y = np.arange(100, 1100, 100)
colors = np.arange(0,100, 10)
self.plt.scatter(x, y, c=colors, cmap='BrBG_r', alpha=0.5)
self.plt.colorbar()
self.plt.show()
# 饼图
def pie_mat(self):
# pie
x = ['a','b', 'c', 'd']
y = np.array([20, 30, 40, 10])
self.plt.pie(y, labels=x, autopct='%2.f%%', explode=(0.1, 0, 0, 0), colors=['#AABBAA', '#f0b8f0', '#008001', '#899999'])
self.plt.show()
# 网格线图
def grid_mat(self):
# grid
x = np.arange(0, 20, 2)
y = np.arange(20, 40, 2)
self.plt.title("test")
self.plt.xlabel("x - test")
self.plt.ylabel("y - test")
self.plt.plot(x, y)
self.plt.grid(axis='x') # 默认axis=both(xy轴都有)
self.plt.show()https://www.runoob.com/matplotlib/matplotlib-tutorial.html
本文含有隐藏内容,请 开通VIP 后查看