微信扫一扫打赏支持

matplotlib_200730系列---11、Subplot 分格显示

matplotlib_200730系列---11、Subplot 分格显示

一、总结

一句话总结:

method 1:subplot2grid:axl=plt.subplot2grid((3,4),(0,0),colspan=4,rowspan=1)
method 2:gridspec:gs=gridspec.GridSpec(3,3); ax1 = plt.subplot(gs[0,:])
method 3:easy to define structure:f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)

 

 

二、Subplot 分格显示

博客对应课程的视频位置:

 

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec
In [42]:
# method 1:subplot2grid
plt.figure()
axl=plt.subplot2grid((3,4),(0,0),colspan=4,rowspan=1)
axl.plot([1,2],[1,2])
axl.set_title('axl_title')
ax2=plt.subplot2grid((3,4),(1,0),colspan=2,)
ax3=plt.subplot2grid((3,4),(1,2),rowspan=2)
ax4=plt.subplot2grid((3,4),(2,0))
ax5=plt.subplot2grid((3,4),(2,1))
ax6=plt.subplot2grid((3,4),(2,3))
plt.tight_layout()
plt.show()
In [48]:
# method 2:gridspec
plt.figure()
gs=gridspec.GridSpec(3,3)

ax1 = plt.subplot(gs[0,:])
ax2 = plt.subplot(gs[1,:2])
ax3 = plt.subplot(gs[1:,2])
ax4 = plt.subplot(gs[-1,0])
ax5 = plt.subplot(gs[-1,-2])

plt.tight_layout()
plt.show()
In [49]:
# method 3:easy to define structure

# 共享x轴和y轴
# 改title啥的就用f改即可
# 可以在图上很明显的看到共享x轴和共享y轴的效果
f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[2,1])

plt.tight_layout()
plt.show()
In [ ]:
 

 

 
posted @ 2020-07-31 03:29  范仁义  阅读(212)  评论(0编辑  收藏  举报