2016-10-28 265 views
4

調整插曲佈局我才明白,matplotlibtight_layout()不能應用於由大熊貓產生的地塊。與大熊貓

這是我運行的代碼:我結束了以下錯誤

  0   1   2   3   4 
A 0.039895 0.960105  NaN  NaN  NaN 
D 0.030418 0.969582  NaN  NaN  NaN 
E 0.037345 0.962655  NaN  NaN  NaN 
F 0.061522 0.938478  NaN  NaN  NaN 
G 0.047163 0.952837  NaN  NaN  NaN 
H 0.026423 0.000000 0.000000 0.973577  NaN 

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False, 
          layout=(2,4), sharex=True, sharey=True) 
plt.tight_layout() 

AttributeError: 'NoneType' object has no attribute 'is_bbox' 

我也認爲,這是關係到張貼在github上一個類似的問題: DataFrame.hist() does not get along with matplotlib.pyplot.tight_layout() #9351

因此,我正在尋找一個基於subplots_adjust(*args, **kwargs)的解決方法。最重要的是,我試圖調整hspace參數。但是,在調用pandasplot函數時,不接受這些關鍵字參數。

有什麼建議嗎?

+1

您可以發佈產生錯誤的代碼。也就是說,你怎麼叫plt。如果您沒有任何子插槽(即使它只有一個子插槽),則無法調用subplots_adjust()方法。 –

回答

3

tight_layout()絕對適合熊貓!

而不tight_layout()

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False, 
          layout=(3, 2), sharex=True, sharey=True) 
# plt.tight_layout() 

enter image description here


tight_layout()

df.T.plot(kind='bar', subplots=True, width=0.7, legend=False, 
          layout=(3, 2), sharex=True, sharey=True) 
plt.tight_layout() 

enter image description here

+0

即使我只是使用python 2.7和pandas 0.18.0在Mac上將Spyder複製到代碼中,我也會得到與上述相同的錯誤。 – Fourier

+0

@Fourier有趣!必須是版本問題。我使用python 3.5和pandas 0.19。我會再次檢查不同的版本。 – piRSquared

+1

好的,如果你想關閉這個問題,請繼續。我很抱歉,我發現了這個問題。我有一個圖形佈局,其中包含比將用於繪圖更多的子圖,而熊貓會自動刪除它們。當使用tight_layout()時,我得到上面所述的錯誤。但是,如果僅使用適合圖的數量的佈局矩陣,則它將起作用! – Fourier

1

正如piRSquared指出的tight_layout()絕對有效。但是,佈局應該完全符合子圖的數量。

熊貓自動刪除空的子圖,因此圖形佈局超過所需子圖的數量將導致上述錯誤。

這是我自己的問題解決方案。儘可能簡單。

使用plt.subplots_adjust()可以在繪製圖形後輕鬆調整所需的參數。