簡體   English   中英

將子圖添加到現有圖中?

[英]Add subplot to existing figure?

我正試圖掌握matplotlib。 當網格形狀已經存在時,我很難理解如何添加子圖。

因此,例如,如果我創建一個帶有2 * 2網格子圖的圖形,我該如何添加第5或第6個。 即如何更改幾何以適應另一個子圖。 如果我這樣做:

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey=True, facecolor='red', \
dpi = 100, edgecolor = 'red', linewidth = 5.0, frameon = True)
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')

然后我想在下面添加另一個子圖:

f.add_subplot(3, 2, 5)

然后新的子圖與第四個重疊,當我希望它明顯位於下方時。 我需要更改幾何圖形嗎? 如果是這樣,怎么樣? 或者它只是一個位置的東西?

更普遍的是,帶有子情節的** kwargs會發生什么? 如果有人可以幫我解決如何開始使用它們,那也會非常方便。

我認為你應該改變繪圖布局幾何的直覺是正確的。 您首先指定(2,2)幾何形狀,即2行和2列,因此通過常規方法自然添加第5個圖可能會導致一些問題。 有多種方法可以解決這個問題,但其中一個更簡單的解決方案就是通過使用第三行圖來給自己一個更大的子圖網格 - 使用(3,2)幾何,如下例所示:

%pylab inline
import numpy as np
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)
#probably easiest to add a third row of plots (3,2) at this stage:
f, ((ax1, ax2), (ax3, ax4),(ax5,ax6)) = plt.subplots(3, 2, sharex=True, sharey=True,    facecolor='red', \
dpi = 100, edgecolor = 'red', linewidth = 5.0, frameon = True)
ax1.plot(x, y)
ax1.set_title('Sharing x per column, y per row')
ax2.scatter(x, y)
ax3.scatter(x, 2 * y ** 2 - 1, color='r')
ax4.plot(x, 2 * y ** 2 - 1, color='r')
#now the fifth plot shouldn't clobber one of the others:
ax5.plot(x,y**3,color='g')

在此輸入圖像描述

如果你想讓第五個圖占據整個底行,而不是在那里有一個空白的第6個圖,你可以使用更高級的選項,比如gridspec文檔中描述的matplotlib.gridspec

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM