2013-07-02 94 views
8

我使用autofmt_xdate以可讀方式繪製長x軸標籤。問題是,當我想要組合不同的子圖時,其他子圖的x軸標籤消失,對於下圖中最左側的子圖(兩行高),我不理解。有沒有辦法阻止autofmt_xdate熄滅其他x軸標籤?或者還有另一種旋轉標籤的方法嗎?正如你所看到的,我也嘗試了xticks和「旋轉」,但結果並不令人滿意,因爲標籤圍繞其中心旋轉,導致亂標籤。產生以下情節autofmt_xdate刪除所有子圖的x軸標籤

腳本:

from matplotlib import pyplot as plt 
from numpy import arange 
import numpy 
from matplotlib import rc 

rc("figure",figsize=(15,10)) 
#rc('figure.subplot',bottom=0.1,hspace=0.1) 
rc("legend",fontsize=16) 
fig = plt.figure() 


Test_Data = numpy.random.normal(size=20) 

fig = plt.figure() 
Dimension = (2,3) 
plt.subplot2grid(Dimension, (0,0),rowspan=2) 
plt.plot(Test_Data) 
plt.subplot2grid(Dimension, (0,1),colspan=2) 
for i,j in zip(Test_Data,arange(len(Test_Data))): 
    plt.bar(i,j) 
plt.legend(arange(len(Test_Data))) 
plt.subplot2grid(Dimension, (1,1),colspan=2) 
xticks = [r"%s (%i)" % (a,b) for a,b in zip(Test_Data,Test_Data)] 
plt.xticks(arange(len(Test_Data)),xticks) 
fig.autofmt_xdate() 
plt.ylabel(r'$Some Latex Formula/Divided by some Latex Formula$',fontsize=14) 
plt.plot(Test_Data) 
#plt.setp(plt.xticks()[1],rotation=30) 
plt.tight_layout() 
#plt.show() 

Figure created by script

回答

6

這實際上是autofmt_xdate方法的一個特徵。從autofmt_xdate方法的文檔:

日期ticklabels經常重疊,所以它是旋轉它們並對齊它們是有用的。此外,一個常見用例是一些共享xax的子圖,其中x軸是日期數據。蜱標通常很長,並且它有助於將它們旋轉到底部的子圖上,並在其他子圖上關閉它們,並關閉xlabels。

如果你想旋轉只有右下角的次要情節的xticklabels,使用

plt.setp(plt.xticks()[1], rotation=30, ha='right') # ha is the same as horizontalalignment 

這旋轉ticklabels 30度,右對齊它們(同樣的結果使用autofmt_xdate時)的右下方使其他兩個小插圖保持不變。