matplotlib的简单入门
一、matplotlib是什么?
Matplotlib 是一个在 python 下实现的类 matlab 的纯 python 的第三方库,旨在用 python实现 matlab 的功能,是python下最出色的绘图库。其风格跟 matlab 相似,同时也继承了 python 的简单明了
要使用matplotlib得先安装 numpy 库 (一个python下数组处理的第三方库,可以很方便的处理矩阵,数组)
matplotlib 对于图像美化方面比较完善,可以自定义线条的颜色和样式,可以在一张绘图纸上绘制多张小图,也可以在一张图上绘制多条线, 可以很方便地将数据可视化并对比分析
Matplotlib模块依赖于NumPy和tkinter模块,可以绘制多种形式的图形,包括线图、直方图、饼图、散点图等,图形质量满足出版要求,是数据可视化的重要工具
Matplotlib中应用最广的是matplotlib.pyplot模块。Pyplot提供了一套和Matlab类似的绘图API,使得Matplotlib的机制更像Matlab。我们只需要调用Pyplot模块所提供的函数就可以实现快速绘图并设置图表的各个细节
本文可能有点长,将循序渐进的学习。后续也可能补充实战,请耐心观看
二、使用步骤
1.画布和子图
代码如下:
- figure用来创建画布。
- add_subplot用来创建子图,参数前两个是做出一个二行三列的大图,其中的每一个格都是一个独立的子图,第三个参数则是子图中的第几个,以从左到右的顺序一次排开。
- subplots_adjust是调节子图之间的间距
# 导入绘图模块
import matplotlib.pyplot as plt
#中文乱码和正负号的处理
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
plt.subplots_adjust(wspace=1,hspace=1)
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
ax4 = fig.add_subplot(2,3,4)
ax5 = fig.add_subplot(2,3,5)
ax6 = fig.add_subplot(2,3,6)
ax1.bar(range(5),[1.5,2,3.5,-1,1.6])
ax2.plot(range(7))
ax3.scatter(range(7),range(8,15))
ax4.pie(range(5))
ax5.barh(range(5),[1.5,2,3.5,-1,1.6])
效果如下:
2.轴和标题
代码如下:
为子图添加配置项使用set
参数解释
- xlim:配置x轴的坐标轴范围
- ylim:配置y轴的坐标轴范围
- title:为图添加标题
- xlabel:x轴的标签
- ylabel:y轴的标签
# 导入绘图模块
import matplotlib.pyplot as plt
#中文乱码的处理
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
ax1 = fig.add_subplot(2,3,1)
ax2 = fig.add_subplot(2,3,2)
ax3 = fig.add_subplot(2,3,3)
ax4 = fig.add_subplot(2,3,4)
ax5 = fig.add_subplot(2,3,5)
ax6 = fig.add_subplot(2,3,6)
ax1.bar(range(5),[1.5,2,3.5,-1,1.6])
ax1.set(xlim=[0.5, 4.5], ylim=[-2, 8], title='An Example Axes',ylabel='Y-Axis', xlabel='X-Axis')
ax2.plot(range(7))
ax2.set(xlim=[0, 6], ylim=[0, 10], title='An Example Axes',ylabel='Y-Axis', xlabel='X-Axis')
ax3.scatter(range(7),range(8,15))
ax3.set(xlim=[0, 6], ylim=[5, 16], title='An Example Axes',ylabel='Y-Axis', xlabel='X-Axis')
ax4.pie(range(5))
ax5.barh(range(5),[1.5,2,3.5,-1,1.6])
ax5.set(xlim=[-1, 6], ylim=[0, 6], title='An Example Axes',ylabel='Y-Axis', xlabel='X-Axis')
结果如图:
3.图像之间的填充
fill_between函数,参数分别是自变量x,函数结果y1,y2,颜色。
#方法1:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,200)
y1 = 2*x
y2 = 3*x
y3 = x*x
fig = plt.figure()
ax = fig.subplots()
ax.fill_between(x,y1,y2,color='yellow')
ax.fill_between(x,y1,y3,color='red')
ax.plot(x,y3)
#方法2:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,200)
data={
'x':x,
'y1' : 2*x,
'y2' : 3*x,
'y3' : x*x
}
fig = plt.figure()
ax = fig.subplots()
ax.fill_between('x','y1','y2',color='yellow',data=data)
ax.fill_between('x','y1','y3',color='red',data=data)
ax.plot(x,y3)
结果如图:
4.图例
图例实用的是legend函数,此函数不能写在set中,需要给子图实例设置。
# 导入绘图模块
import matplotlib.pyplot as plt
import numpy as np
#中文乱码的处理
plt.rcParams['font.sans-serif'] =['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False
fig = plt.figure()
plt.subplots_adjust(wspace=0.2,hspace=0.2)
x=np.arange(0,np.pi,0.1)
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(x,x*x)
ax1.plot(x,x*x*x)
ax1.set(xlim=[0,1],ylim=[0,1],xticks=[0.25,0.5,0.75,1],xlabel='x轴',ylabel='y轴',title="子图1")
ax1.legend(['x*x','x*x*x'])
ax2.plot(x,np.sin(x))
ax2.plot(x,np.cos(x))
ax2.set(xlim=[-0.5*np.pi,np.pi*1.5],ylim=[-1.5,1.5],xlabel='x轴',ylabel='y轴',title='sin/cos')
ax2.legend(['sin','cos'])