2011-11-18 122 views

回答

11

不完全。 (看看the new matplotlib.pyplot.tight_layout() function與其他類似的東西雖然......)

但是,長x-tick標籤的常用技巧就是旋轉它們。

例如,如果我們有一些重疊xticklabels:

import matplotlib.pyplot as plt 

plt.plot(range(10)) 
labels = [15 * repr(i) for i in range(10)] 
plt.xticks(range(10), labels) 
plt.show() 

enter image description here

我們可以旋轉,使它們更易於閱讀。(關鍵是rotation=30plt.tight_layout()調用只是調整繪圖的底部邊緣以使標籤不會脫離底部邊緣。)

import matplotlib.pyplot as plt 

plt.plot(range(10)) 
labels = [10 * repr(i) for i in range(10)] 
plt.xticks(range(10), labels, rotation=30) 
plt.tight_layout() 
plt.show() 

enter image description here

默認情況下,刻度標籤以刻度爲中心。對於旋轉的刻度,通常更有意義的是讓標籤的左邊或右邊從刻度開始。

例如,像這樣(右側,正轉):

import matplotlib.pyplot as plt 

plt.plot(range(10)) 
labels = [10 * repr(i) for i in range(10)] 
plt.xticks(range(10), labels, rotation=30, ha='right') 
plt.tight_layout() 
plt.show() 

enter image description here

還是這個(左側,負旋轉):

import matplotlib.pyplot as plt 

plt.plot(range(10)) 
labels = [10 * repr(i) for i in range(10)] 
plt.xticks(range(10), labels, rotation=-30, ha='left') 
plt.tight_layout() 
plt.show() 

enter image description here

+0

喬,如果我不想要任何數字並贏回空間,那麼製作的畫布沒有邊距,只有正方形(理想上只是圖形,沒有線條)?我試圖在gtk容器中顯示它,並且我需要它精確定位。 'a.axis('off')'只是刪除了數字,'a.margins(0,tight = True)'似乎不起作用......幫助! – yPhil

+1

@xaccrocheur - 「ax.margins」控制數據限制的自動縮放工作方式,而不是圖的「外側」邊距。如果你想控制軸所佔的畫布的大小,可以1)用'ax = fig.add_axes([0,0,1,1])'手工製作佔據整個圖形的軸,或者2)使用'subplots_adjust'來改變你的子圖的「外部」邊界(例如'fig.subplots_adjust(0,0,1,1)') –