Matliotlib库是Python优秀的数据可视化第三方库。
Matliotlib库的效果见:http://matplotlib.org/gallery.html
Matplotlib库由各种可视化类构成,内部结构复杂,受Matlab启发。
matplotlib.pyplot是绘制各类可视化图形的命令字库,相当于快捷方式。导入方式如下:
import matplotlib.pyplot as plt
实例:使用Matplotlib库绘图
import matplotlib.pyplot as plt
plt.plot([3,1,4,5,2])
plt.ylabel("Grade")
plt.savefig('test',dpi=600) #PNG文件
plt.show()
plt.savefig()将输出图形存储为文件,默认PNG格式,可以通过dpi修改输出质量。
pyplot的绘图区域
plt.subplot(nrows,ncols,plot_number)
在全局绘图区域中创建一个分区体系,并定位到一个子绘图区域。
pyplot的plot()函数
plt.plot(x, y, format_string, **kwargs)
x:X轴数据,列表或数组,可选(绘制多条曲线时,各条曲线的x不能省略)
y:Y轴数据,列表或数组
format_string:控制曲线的格式字符串(由颜色字符、风格字符和标记字符组成),可选
**kwargs:第二组或更多(x, y, format_string)
color:控制颜色,color='green'
linestyle:线条风格,linestyle='dashed'
marker:标记风格,marker='o'
markerfacecolor:标记颜色,markerfacecolor='blue'
marksize,标记尺寸,markerfacecolor=20
pyplot的子绘图区域:plt.subplot2grid()
plt.subplot2grid(GridSpec, CurSpec, colspan=1, rowspan=1)
理念:设定网格,选中网格,确定选中行列区域数量,编号从0开始
GridSpec为二元组(m,n),表示将网格分为m行n列
CurSpec为二元组(a,b),表示选择第几个网格,索引从0开始
colspan和rowspan表示合并几个单元格,默认为1(不合并)
import matplotlib
import matplotlib.gridspec as gridspec
matplotlib.rcParams['font.size']=6
gs = gridspec.GridSpec(3,3)
ax1 = plt.subplot(gs[0, :])
ax2 = plt.subplot(gs[1, :-1])
ax3 = plt.subplot(gs[1:, -1])
ax4 = plt.subplot(gs[2, 0])
ax5 = plt.subplot(gs[2, 1])