日期:2024.03.12
内容:将matplotlib的常用方法做一个记录,方便后续查找。
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(20,8),dpi=80)
x = range(2,26,2)
y = [15,13,14.5,17,20,25,26,26,27,22,18,15]
x_ticks01 = [ i for i in range(1,25)]
y_ticks01 = range(min(y),max(y) + 1)
plt.xticks(x_ticks01)
plt.yticks(y_ticks01)
plt.plot(x,y)
plt.savefig('./test01.png')
plt.savefig('./test01.svg')
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/647603ba2af1ab499ae9d188516c2a17.png)
from matplotlib import pyplot as plt
import random
fig = plt.figure(figsize=(20,10),dpi=80)
plt.rcParams['font.sans-serif'] = ['Simhei']
x = list(range(1,121,1))
y = [random.randint(20,25) for i in range(120)]
plt.title("时间-温度对应图(10-12点)",fontsize = 18)
plt.xlabel("时间",fontsize = 18)
plt.ylabel("温度",fontsize = 18)
x_ticks01 = x
x_ticks01Label = [f"9点{i}"for i in range(00,60)]
x_ticks01Label += [f"10点{i}"for i in range(00,60)]
y_ticks01 = range(min(y),max(y) + 1)
plt.xticks(x_ticks01[::10],x_ticks01Label[::10],rotation = 45,fontsize = 18)
plt.yticks(y_ticks01,fontsize = 18)
plt.plot(x,y)
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4dc915f45e995e48dfb6ccda2b5a8277.png)
from matplotlib import pyplot as plt
import random
fig = plt.figure(figsize=(20,10),dpi=80)
plt.rcParams['font.sans-serif'] = ['Simhei']
plt.rcParams.update({'font.size': 25})
x = list(range(11,31))
y1 = [1,0,1,1,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y2 = [1,0,1,1,2,2,1,2,1,1,1,2,1,2,1,1,1,1,1,1]
x_ticks01 = x
x_ticks01Label = [f"{i}岁"for i in x]
plt.xticks(x_ticks01,x_ticks01Label,rotation = 45,fontsize = 18)
plt.plot(x,y1,label = "中文",color = 'r',linestyle="--",linewidth = "5")
plt.plot(x,y2,label = "b")
plt.legend(loc='upper left')
plt.grid(0.5)
plt.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e82393ff2693d4dcd5c1049fdfeb3e35.png)