matplotlib畫圖總結--多子圖布局


1、subplot布局

subplot(nrows, ncols, index, **kwargs)

subplot(pos, **kwargs)

subplot(ax)

x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1')

plt.subplot(222)
plt.scatter(x, values)

plt.subplot(223)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

上面的圖第三張只占半個圖紙長度,不美觀。那么使用subplot怎么畫非對陣圖呢?重新定義子圖的分布行列即可。

 plt.subplot(212) 或plt.subplot(2,1,2)把圖紙分為2行1列,當前子圖是第二個。

x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1')
plt.subplot(222)
plt.scatter(x, values)
plt.subplot(212)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

先把圖紙分為22列,先畫圖1和圖3,然后再把圖紙划分為12列,對第二列繪圖。

x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1')
plt.subplot(223)
plt.scatter(x, values)
plt.subplot(1,2,2)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

2、subplots布局

matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw) :創建一個圖形和一組子圖。

fig, ax = plt.subplots(2, 3)
fig.tight_layout()
ax[0].text(0.5,0.5, 'sss')
plt.show()

3、subplot2grid布局

matplotlib.pyplot.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)

shape : sequence of 2 ints

loc : sequence of 2 ints

rowspan : int

Number of rows for the axis to span to the right.

colspan : int

 

Number of columns for the axis to span downwards.

fig = plt.figure()
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))
plt.show()

x = np.arange(1,10)
fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=1, title = 'plt1')
ax1.plot(x,x*x)
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2, title = 'plt2')
ax2.plot(x,x*x)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=3, title = 'plt3')
ax3.plot(x,x*x)
ax4 = plt.subplot2grid((3, 3), (2, 0), title = 'plt4')
ax4.plot(x,x*x)
ax5 = plt.subplot2grid((3, 3), (2, 1), title = 'plt5')
ax5.plot(x,x*x)
ax6 = plt.subplot2grid((3, 3), (2, 2), title = 'plt6')
ax6.plot(x,x*x)
plt.legend()
plt.suptitle('subplot2grid figure',  x=0.5,y=0.95, ha='center', va='center', fontsize=15)
plt.show()

可見上面的圖,x周名稱和附件的圖互相干涉。需要縮小圖或者加大間隙。

使用layout函數plt.tight_layout()。但是圖的title和第一行干涉。

 plt.tight_layout(rect=[0, 0, 1, 0.95]) 或者添加一句fig.subplots_adjust(top=0.85)即可。

matplotlib.pyplot.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)

其中rect可以這樣定義:[left, bottom, right, top] in normalized (0, 1) figure coordinates,所以可以定義right和top,進行圖的縮放。

A rectangle (left, bottom, right, top) in the normalized figure coordinate that the whole subplots area (including labels) will fit into. Default is (0, 0, 1, 1).

 圖的縮放,做個實驗plt.tight_layout(rect=[0, 0, 0.7, 0.5])


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM