2016-04-05 115 views
0

我正在網格上繪製幾個數據集。時間序列延續了一年,所以我想要有一個月份年份的刻度標記,但刻度標籤很快會彼此重疊。爲了解決這個問題,我試圖旋轉xticklables。但是,這不符合預期。這是一個非最小的工作示例:xticklabels在旋轉後消失

from __future__ import division, print_function 

import pandas as pd, numpy as np 
from pylab import * 

index = pd.date_range('20140901','20150901',freq='1H') 
data = np.sin(np.pi*np.arange(index.shape[0])/180) 
df = pd.DataFrame(index=index,data=data,columns=list("A")) 

fig = figure(figsize=(3.25,4.0)) 
gs = GridSpec(5,2) 
gs.update(left=0.08, right=0.925, top=0.95, bottom=0.05, hspace=0.0, wspace=0.0) 

ax0 = subplot(gs[0]) 
ax1 = subplot(gs[1]) 
ax2 = subplot(gs[2]) 
ax3 = subplot(gs[3]) 
ax4 = subplot(gs[4]) 
ax5 = subplot(gs[5]) 
ax6 = subplot(gs[6]) 
ax7 = subplot(gs[7]) 

for axis in xrange(8): 
    ax = eval('ax'+str(axis)) 

    ax.plot(df.index,df.A,'k') 

    # format y-axes 
    if axis in np.arange(1,8,2): 
     ax.set_yticks(np.arange(-1.5,1.5,0.50),minor=False) 
     ax.set_yticks(np.arange(-1.5,1.5,0.25),minor=True) 
     ax.set_yticklabels([]) 

    elif axis in np.arange(2,8,2): 
     ax.set_yticks(np.arange(-1.5,1.5,0.50),minor=False) 
     ax.set_yticks(np.arange(-1.5,1.5,0.25),minor=True) 

    elif axis==0: 
     ax.set_yticks(np.arange(-1.5,1.6,0.50),minor=False) 
     ax.set_yticks(np.arange(-1.5,1.5,0.25),minor=True) 

    ax.set_ylim([-1.5,1.5]) 

    # format x-axes 
    if axis in np.arange(0,6): 
     ax.set_xticklabels([]) 
     ax.xaxis.set_minor_locator(dates.MonthLocator(interval=1)) 
     ax.xaxis.set_major_locator(dates.MonthLocator(interval=3)) 

    elif axis in [6,7]: 
     ax.xaxis.set_minor_locator(dates.MonthLocator(interval=1)) 
     ax.xaxis.set_major_locator(dates.MonthLocator(interval=3)) 
     ax.xaxis.set_major_formatter(dates.DateFormatter('%b-%Y')) 

    ax.set_xlim([dt.datetime(2014,9,1),dt.datetime(2015,9,1)]) 

    ax.tick_params(axis='both',which='major',direction='in',length=4,width=1,labelsize=8) 
    ax.tick_params(axis='both',which='minor',direction='in',length=2,width=1)  


    # insert text boxes 
    ax.annotate('A', 
       xycoords='axes fraction', 
       xy=(0.05,0.75), 
       horizontalalignment='left', 
       verticalalignment='bottom', 
       fontsize=8) 

ax6.set_xticklabels(ax6.get_xticklabels(),rotation=45) 
ax7.set_xticklabels(ax7.get_xticklabels(),rotation=45) 

如果我按原樣運行腳本,則最下面的x軸上將不會有tick標籤。但是,如果我註釋掉最後兩行,請運行腳本,然後手動輸入旋轉按預期完成的最後兩行。我研究了幾種旋轉刻度標籤的方法,但都沒有工作(即它們都有相同的「消失」問題)。有誰知道這是爲什麼發生,以及如何解決它?

的Python 2.7,Matplotlib 1.5.1,熊貓0.18.0

enter image description hereenter image description here

+0

向我們展示您的情節。 – Hun

+0

完成。第一個圖是通過運行腳本生成的,沒有最後兩行評論,第二個是通過評論最後兩行生成的,運行腳本,然後手動輸入最後兩行。您可以通過運行發佈的腳本來重新創建這些數字。 – tnknepp

+0

順便說一句,我爲可憐的圖像分辨率道歉,但所有需要看到的是,第一張圖像上沒有顯示刻度標籤。 – tnknepp

回答

1

改變你這樣的計劃會奏效。

# format x-axes 
if axis in np.arange(0,6): 
    ax.set_xticklabels([]) 
    ax.xaxis.set_minor_locator(dates.MonthLocator(interval=1)) 
    ax.xaxis.set_major_locator(dates.MonthLocator(interval=3)) 

elif axis in [6,7]: 
    ax.set_xticklabels(index.strftime('%b-%Y'),rotation=45)  # Add this line 
    ax.xaxis.set_minor_locator(dates.MonthLocator(interval=1)) 
    ax.xaxis.set_major_locator(dates.MonthLocator(interval=3)) 
    # ax.xaxis.set_major_formatter(dates.DateFormatter('%b-%Y')) # This not needed 
+0

精美的作品。你知道爲什麼我的原始代碼導致標籤消失嗎?也許只是在pylab中的一個夸克... – tnknepp

+0

順便說一句,我編輯了你的答案,以包含ax.set_xticklabels中的旋轉= 45參數,因爲標籤旋轉是問題的一部分。 – tnknepp