2011-06-30 6 views
167

this questionとよく似ていますが、私の数字は必要なだけ大きくても差があります。matplotlibの多くのサブプロットでサブプロットサイズ/スペーシングを改善してください

私はmatplotlibに垂直に積み重ねられたプロットを一括して生成する必要があります。結果はfigsaveを使用して保存され、ウェブページで表示されるので、サブプロットが重なり合わないように間隔を置いている限り、最終画像の高さは気にしません。

数字がどれほど大きくても、サブプロットは常に重なっているようです。

私のコードは、現在、あなたはコール署名

(source)サブプロット間の間隔変更する plt.subplots_adjustを使用することができます

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

次のようになります。

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

調整ボタンが表示されません。私はジュピターのノートに入っていますが。私は%matplotlibインラインと%matplotlibノートブックを試しました。 –

36

によって制御されている私はsubplots_adjust(HSPACE = 0.001)は私のために働いてしまった何であることを見出しました。 space = Noneを使用すると、各プロットの間に空白が残ります。しかし、それをゼロに非常に近いものに設定することは、それらを一直線にするように思われます。私がここにアップロードしたのは、最もエレガントなコードではありませんが、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

+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()

関連する問題