2011-06-30 1229 views
167

this question非常相似,但區別在於我的圖形可以大到需要的大小。在matplotlib中改進了子圖大小/間距與許多小圖

我需要在matplotlib中生成一大堆垂直堆積的圖。結果將使用無花果保存並在網頁上查看,所以我不在乎最終圖像有多高,只要子圖間隔不大,因此它們不會重疊。

無論我有多大的數字,子圖總是看起來重疊。

我的代碼目前看起來像

import matplotlib.pyplot as plt 
import my_other_module 

titles, x_lists, y_lists = my_other_module.get_data() 

fig = plt.figure(figsize=(10,60)) 
for i, y_list in enumerate(y_lists): 
    plt.subplot(len(titles), 1, i) 
    plt.xlabel("Some X label") 
    plt.ylabel("Some Y label") 
    plt.title(titles[i]) 
    plt.plot(x_lists[i],y_list) 
fig.savefig('out.png', dpi=100) 

回答

157

您可以使用plt.subplots_adjust改變次要情節之間的間距(source)

調用簽名:

subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None) 

參數含義(並建議默認值)是:

left = 0.125 # the left side of the subplots of the figure 
right = 0.9 # the right side of the subplots of the figure 
bottom = 0.1 # the bottom of the subplots of the figure 
top = 0.9  # the top of the subplots of the figure 
wspace = 0.2 # the amount of width reserved for blank space between subplots 
hspace = 0.2 # the amount of height reserved for white space between subplots 

實際的缺省值由RC文件

+0

我試過用hspace搞亂,但是增加它似乎只是讓所有的圖形變小而沒有解決重疊問題我也試過玩其他參數,但我不知道左邊,右邊,底部和頂部是什麼實際指定那裏。 – mcstrother

+25

@mcstrother如果你在顯示一個圖形後點擊'調整'按鈕,你可以交互式地改變所有這些參數中的6個,然後一旦你找到有效的代碼就將它們複製到代碼中。 –

+0

我沒有看到調整按鈕。雖然我在Jupyter筆記本中。我嘗試了%matplotlib內聯和%matplotlib筆記本。 –

36

控制,我發現subplots_adjust(HSPACE = 0.001)是什麼最終爲我工作。當我使用space = None時,每個plot之間仍有空白。將它設置爲非常接近零的值似乎迫使它們排隊。我在這裏上傳的並不是最優雅的一段代碼,但你可以看到hspace是如何工作的。

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.ticker as tic 

fig = plt.figure() 

x = np.arange(100) 
y = 3.*np.sin(x*2.*np.pi/100.) 

for i in range(5): 
    temp = 510 + i 
    ax = plt.subplot(temp) 
    plt.plot(x,y) 
    plt.subplots_adjust(hspace = .001) 
    temp = tic.MaxNLocator(3) 
    ax.yaxis.set_major_locator(temp) 
    ax.set_xticklabels(()) 
    ax.title.set_visible(False) 

plt.show() 

enter image description here

227

嘗試使用plt.tight_layout

作爲一個簡單的例子:

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(nrows=4, ncols=4) 
fig.tight_layout() # Or equivalently, "plt.tight_layout()" 

plt.show() 

沒有佈局緊湊

enter image description here


緊佈局 enter image description here

+1

這真的很酷。 – Lokesh

+5

如果你顯示你應該在你的劇情代碼後執行這個,它會更清楚一些,但是在show()之前() – MtRoad

+0

這對我有效!謝謝! –

25
import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(10,60)) 
plt.subplots_adjust(...) 

plt.subplots_adjust方法:

def subplots_adjust(*args, **kwargs): 
    """ 
    call signature:: 

     subplots_adjust(left=None, bottom=None, right=None, top=None, 
         wspace=None, hspace=None) 

    Tune the subplot layout via the 
    :class:`matplotlib.figure.SubplotParams` mechanism. The parameter 
    meanings (and suggested defaults) are:: 

     left = 0.125 # the left side of the subplots of the figure 
     right = 0.9 # the right side of the subplots of the figure 
     bottom = 0.1 # the bottom of the subplots of the figure 
     top = 0.9  # the top of the subplots of the figure 
     wspace = 0.2 # the amount of width reserved for blank space between subplots 
     hspace = 0.2 # the amount of height reserved for white space between subplots 

    The actual defaults are controlled by the rc file 
    """ 
    fig = gcf() 
    fig.subplots_adjust(*args, **kwargs) 
    draw_if_interactive() 

fig = plt.figure(figsize=(10,60)) 
fig.subplots_adjust(...) 

圖片的大小很重要。

「我試過用hspace搞亂,但增加它似乎只是使所有圖形變小而不解決重疊問題。「

因此,使更多的空白,並保持子地塊面積合計圖像需要更大。

+0

圖片的大小很重要,更大的圖片大小可以解決這個問題!設置'plt.figure(figsize =(10,7))',圖片的大小將是'2000 x 1400' pix – Belter

7

你可以嘗試subplot_tool()

plt.subplot_tool() 
+0

這節省了我的微調負載,從來不知道它退出謝謝! –

+0

請參閱https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplot_tool.html –

-1

不需要過度工程師在這裏,只是使用plt.tight_layout()