美文网首页数据蛙数据分析每周作业
python数据分析之Matplotlib库入门

python数据分析之Matplotlib库入门

作者: 木子兜兜 | 来源:发表于2019-01-06 20:17 被阅读2次

Matplotlib库由各种可视化类构成,matplotlib.pyplot是绘制各类可视化图形的命令子库,相当于其快捷方式。
import matplotlib.pyplot as plt 引入模块别名
一.plt.plot(x,y)
当有两个以上参数时,按照X轴和Y轴顺序绘制数据点,e.g.plt.plot([0,2,4,6,8],[3,1,4,5,2])
当只有一个输入列表或数组时,参数会被当做是Y轴,X轴会以索引自动生成,e.g.plt.plot([3,1,4,5,2]),系统会按
plt.plot([0,1,2,3,4],[3,1,4,5,2])进行绘制数据点。
二.pyplot的绘图区域
plot.subplot(nrows,ncols,plot_number)表示在全局绘图区域中创建一个分区体系,并定位到一个绘图区域
e.g.plt.subplot(3,2,4)逗号可省略,即plt.subplot(324),将画面分成3行,2列,选取第4块区域定位到的绘图区域如下图:


image.png

pyplot的子绘图区域


image.png

1.plt.subplot2grid(GridSpec,CurSpec,colspan=1,rowspan=2)
plt.subplot2grid((3,3),(1,0),colspan=2)
这表示选取的时ax2区域,即先将区域按(3,3)分成9块,然后选择第二行,第一列那块,向横向延伸选取2块
图中选取各区域块的代码如下:
ax1:plt.subplot2grid((3,3),(0,0),colspan=3)
ax2:plt.subplot2grid((3,3),(1,0),colspan=2)
ax3:plt.subplot2grid((3,3),(1,2),rowspan=2)
ax4:plt.subplot2grid((3,3),(2,0))
ax5:plt.subplot2grid((3,3),(2,1))
2.GridSpec类
可以将上面的代码写成:
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])

相关文章

网友评论

    本文标题:python数据分析之Matplotlib库入门

    本文链接:https://www.haomeiwen.com/subject/riherqtx.html