1.Matplotlib库的介绍
Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发,matplotlib.pyplot是绘制各类可视化图形的命令子库,相当与快捷方式。
#plt是引入模块的别名,这是一种约定俗成的引入名称
import matplotlib.pyplot as plt
Matplotlib库小测
1.绘制简单的折线图
import matplotlib.pyplot as plt
plt.plot([3,1,4,5,2])
plt.ylabel('Grade')
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/5b3ab2a5dab47714555d75bc6c3957e0.png)
2.将绘制的图保存成文件
import matplotlib.pyplot as plt
plt.plot([3,1,4,5,2])
plt.ylabel('Grade')
plt.savefig('D://test',dpi=600)
3.同时添加X轴和Y轴的数组绘图
import matplotlib.pyplot as plt
plt.plot([0,2,4,6,8],[3,1,4,5,2])
plt.ylabel('Grade')
plt.axis([-1,10,0,6])
plt.show()
eg:
plt.sublot(3,2,4)
![](https://img-blog.csdnimg.cn/img_convert/af16d00de00eab584ad8a157853d6e29.png)
2.pyplot的plot()函数
![](https://img-blog.csdnimg.cn/img_convert/333de477ab098d28da35c78cd2df90e4.png)
eg:
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(10)
plt.plot(a,a*1.5,a,a*2.5,a,a*3.5,a,a*4.5)
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/536461ac76170714de2188827a48ca33.png)
format_string:控制曲线的格式字符串,可选。由颜色字符、风格字符和标记字符组成。
颜色字符:
![](https://img-blog.csdnimg.cn/img_convert/9394d32f78f9f4a447f19af449fe3aad.png)
风格字符:
![](https://img-blog.csdnimg.cn/img_convert/7f0bfc13f3d74a871f18e139cdd5fa4e.png)
标记字符:
![](https://img-blog.csdnimg.cn/img_convert/d51929acba9a0c507a72eb964721dbc1.png)
![](https://img-blog.csdnimg.cn/img_convert/89e6e37e112dd9879e693d4b44bd7698.png)
![](https://img-blog.csdnimg.cn/img_convert/f061ce279f219e56bc1355a6b5005726.png)
eg:
import numpy as np
import matplotlib.pyplot as plt
a=np.arange(10)
plt.plot(a,a*1.5,'bo-',a,a*2.5,'gx--',a,a*3.5,'r-.',a,a*4.5,'y*:')
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/06618f5f82a3de2b5acdf9fa490e059d.png)
3.pyplot的中文显示
使用rcParams修改字体
raParams的属性:
![](https://img-blog.csdnimg.cn/img_convert/9409e3aa021e2e8508ae29ac7c63849e.png)
中文字体的种类:
![](https://img-blog.csdnimg.cn/img_convert/fb4412a10c03f6f90d3947e90a3ed514.png)
eg:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.family']='Kaiti'
matplotlib.rcParams['font.size']=12
matplotlib.rcParams['font.style']='normal'
a=np.arange(0.0,5.0,0.02)
plt.ylabel('纵轴:振幅')
plt.xlabel('横轴:时间')
plt.plot(a,np.cos(2*np.pi*a),'b--')
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/cf7b1365ceede5d851effe0920efc4b0.png)
在有中文输出的地方增加一个属性:fontproperties
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
a=np.arange(0.0,5.0,0.02)
plt.ylabel('纵轴:振幅',fontproperties='SimHei',fontsize=12)
plt.xlabel('横轴:时间',fontproperties='SimHei',fontsize=12)
plt.plot(a,np.cos(2*np.pi*a),'b--')
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/51f6bc57e4f8a848ec26b969d77b0231.png)
4.pyplot的文本显示函数
![](https://img-blog.csdnimg.cn/img_convert/9f75c0f226895216fab20ac68c4f8430.png)
eg:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
a=np.arange(0.0,5.0,0.02)
plt.plot(a,np.cos(2*np.pi*a),'b--')
plt.ylabel('纵轴:振幅',fontproperties='SimHei',fontsize=12,color='green')
plt.xlabel('横轴:时间',fontproperties='SimHei',fontsize=12)
plt.title('正弦波实例 $y=cos(2\pi x)$',fontproperties='SimHei',fontsize=20,color='red')
plt.text(2,1,r'$\mu=100$',fontsize=15)
plt.axis([-1,6,-2,2])
plt.show()
![](https://img-blog.csdnimg.cn/img_convert/7c5e00c1c1bef2e7266b78aac485be9d.png)
5.pyplot的子绘图区域
subplot2grid()
虽然使用plt.subplot()方法也可以设置绘图区域,但是这种方法过于繁琐。当绘图子区域过多时,实际中更多使用plt.subplot2grid()方法。
plt.subplot2grid(GridSepc,CurSpec,colspan=1,rowspan=1)
理念:设定网格,选中网格,确定选中行列区域数量,编号从0开始
划分区域效果:
plt.subplot2grid((3,3),(0,0),colspan=3)
...
plt.subplot2grid((3,3),(1,0),colspan=2)
...
plt.subplot2grid((3,3),(1,2),colspan=2)
...
plt.subplot2grid((3,3),(2,0))
...
plt.subplot2grid((3,3),(2,1))
...
![](https://img-blog.csdnimg.cn/img_convert/975bf66544e784b6a9ec9128e454a746.png)
2.GridSpec类
使用GridSpec类可以达到和subplot2grid()方法相同的效果。
![](https://img-blog.csdnimg.cn/img_convert/d64512168357883367a6ab09abab5561.png)
6.pyplot的基础图标函数
![](https://img-blog.csdnimg.cn/img_convert/d305de034b9d2ec42d7755e87299f997.png)
![](https://img-blog.csdnimg.cn/img_convert/75cd0948a0a2142ad23abffe90d55227.png)