2014-12-02 49 views
2

我想要生成一個可視化熊貓時間序列的較小圖。但是,自動生成的x-ticks不適應新的大小,並導致重疊的刻度。我想知道如何調整x-ticks的頻率?例如。在這個例子中:如何更改熊貓時間序列圖的x-ticks密度?

figsize(4, 2) 

num = 3000 
X = linspace(0, 100, num=num) 
dense_ts = pd.DataFrame(sin(X) + 0.1 * np.random.randn(num), 
         pd.date_range('2014-01-1', periods=num, freq='min')) 

dense_ts.plot() 

我得到的數字是:

time series plot with overlapping x-ticks

我能夠解決使用Matplotlib日期繪製這個問題,但它不是一個很優雅的解決方案 - 該代碼要求我根據每個案例指定所有輸出格式。

figsize(4, 2) 

from matplotlib import dates 

fig, ax = plt.subplots() 
ax.plot_date(dense_ts.index.to_pydatetime(), dense_ts, 'b-') 
ax.xaxis.set_minor_locator(dates.HourLocator(byhour=range(24), 
              interval=12)) 
ax.xaxis.set_minor_formatter(dates.DateFormatter('%H:%m')) 
ax.xaxis.set_major_locator(dates.WeekdayLocator()) 
ax.xaxis.set_major_formatter(dates.DateFormatter('\n\n%a\n%Y')) 

plt.show() 

matplotlib date solution

我不知道是否存在使用熊貓plotting模塊或可能通過設置一些axes對象屬性來解決這個問題的方法嗎?我試圖玩ax.freq的對象,但沒有真正實現任何東西。

回答

0

你可以通過你想顯示在您的dense_ts.plot()x軸值的列表

dense_ts.plot(xticks=['10:01','22:01'...]) 

爲清楚起見又如

df = pd.DataFrame(np.random.randn(10,3)) 

情節不指定xticks

df.plot(legend=False) 

enter image description here

情節與xticks參數

df.plot(xticks=[2,4,6,8],legend=False) 

enter image description here

+0

HM,這很有趣。使用這個xticks關鍵字參數我有時可以控制一些輸入,但是以一種奇怪的方式 - 例如對於'['00:00']'它打印出06:00,12:00,18:00。對於你提到的10,22的情況,它會永遠循環。 – metakermit 2014-12-02 22:01:27

+0

哇,所以xticks = ['00:00']會給你06:00,12:00等? – 2014-12-02 22:31:32

+0

我又增加了一個例子 – 2014-12-02 22:47:23