matplotlib
1.导包
import matplotlib.pyplot as plt
1.1字体
from pylab import mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"]
2.图形绘制流程
2.1创建画布 plt.figure()
plt.figure(figsize=(),dpi=)
figsize 图的长宽
dpi 图的清晰度
返回fig对象
2.2绘制图形 plt.plot(x,y)
2.3显示图像 plt.show()
plt.figure(figsize=(10,10),dpi = 100)
plt.plot([1,2,3,4,5,6,7],[12,14,16,14,10,18,12])
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GP7nN5sL-1678068839609)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305133404664.png)]
3.基础绘图功能
0.准备数据
1.创建画布
2.绘制图像
3.图像显示
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
plt.figure(figsize=(20,8),dpi=100)
plt.plot(x,y_shanghai)
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6TdR9zCf-1678068839611)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305134816297.png)]
3.1自定义x,y刻度
plt.xticks(x,**kwargs)
x:要显示的刻度值 y同
x = range(60)
y_shanghai = [random.uniform(15,18) for i in x]
plt.figure(figsize=(20,8),dpi=100)
plt.plot(x,y_shanghai)
# 添加xy刻度
x_ticks_label = ["11点{}分".format(i) for i in x]
y_ticks = range(40)
plt.xticks(x[::5],x_ticks_label[::5])
plt.yticks(y_ticks[::5])
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WX7i4xAG-1678068839612)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305142446633.png)]
3.2添加网格
#添加网格
plt.grid(True, linestyle='--',alpha = 0.5)
#alpha:透明度
3.3添加描述信息
#添加描述信息
plt.xlabel("111")
plt.ylabel("温度")
plt.title("温度变化",fontsize=20)
3.4图像保存
plt.savefig("test.png")
3.5在一个坐标系中绘制多个图像
y_shanghai = [random.uniform(15,18) for i in x]
y_beijing = [random.uniform(0,10) for i in x]
plt.figure(figsize=(20,8),dpi=100)
plt.plot(x,y_shanghai)
plt.plot(x,y_beijing)
#使用多次plot可以画多个折现
plt.plot(x,y_beijing,color='r',linestyle='--')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NdHTLzWe-1678068839613)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305170958141.png)]
3.6添加图例
plt.plot(x,y_shanghai,label="shanghai")
plt.plot(x,y_beijing,color='r',linestyle='--',label="beijing")
#label="xxx"
#显示图例
plt.legend(loc="best")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-om8WHF5L-1678068839614)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305172353070.png)]
3.7多个图表显示
#0.准备数据
x = range(60)
y_shanghai = [random.uniform(10,15) for i in x]
y_beijing = [random.uniform(0,5) for i in x]
#1.创建画布
fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(20,8),dpi=100)
#2.绘制图像
axes[0].plot(x,y_shanghai,label="shanghai")
axes[1].plot(x,y_beijing,label="beijing",color='r',linestyle='--')
#2.1添加x,y轴刻度
x_ticks_label = ["11时{}分".format(i) for i in x]
y_ticks = range(40)
#刻度显示
axes[0].set_xticks(x[::5])
axes[0].set_yticks(y_ticks[::5])
axes[0].set_xticklabels(x_ticks_label[::5])
axes[1].set_xticks(x[::5])
axes[1].set_yticks(y_ticks[::5])
axes[1].set_xticklabels(x_ticks_label[::5])
#2.2添加网格显示
axes[0].grid(True,linestyle="--",alpha=0.5)
axes[1].grid(True,linestyle="--",alpha=0.5)
axes[0].set_xlabel("time")
axes[0].set_ylabel("temp")
axes[1].set_xlabel("time")
axes[1].set_ylabel("temp")
#图像保存
plt.savefig("test1")
#显示图例
axes[0].legend(loc="best")
axes[1].legend(loc="best")
#3.显示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-x5S0xJxJ-1678068839615)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305180650953.png)]
3.8sin图像
import numpy as np
x=np.linspace(-10,10,1000)
y=np.sin(x)
plt.figure(figsize=(20,8),dpi=100)
plt.plot(x,y)
plt.grid()
plt.show()
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0qToypEs-1678068839616)(C:\Users\lenovo\AppData\Roaming\Typora\typora-user-images\image-20230305181055478.png)]
4.常见图形种类
4.1折线图
plt.plot(x,y)
4.2散点图
plt.scatter(x,y)
j=range(20)
x = [random.uniform(100,200) for i in j]
y = [random.uniform(50,100) for i in j]
plt.figure(figsize=(20,8),dpi=100)
plt.scatter(x,y)
plt.show()
4.3柱状图
plt.bar(x,width,align=‘center’,**kwargs)
x:需要传递的数据
width:柱状图的宽度
align:每个柱状图的位置对齐方式
{'center','edge'},optional,default:'center'
**kwargs:
color:柱状图颜色
4.4直方图
matplotlib.pyplot.hist(x,bins=None)
bins:组距
4.5饼图
plt.pie(x,labels= ,autopct= ,colors)
x:数量,自动算百分比
labels:每部分名称
autopct:占比显示指定
colors:每部分颜色
edge’},optional,default:‘center’
**kwargs:
color:柱状图颜色
### 4.4直方图
matplotlib.pyplot.hist(x,bins=None)
bins:组距
### 4.5饼图
plt.pie(x,labels= ,autopct= ,colors)
x:数量,自动算百分比
labels:每部分名称
autopct:占比显示指定
colors:每部分颜色