2013-01-13 23 views
2

我有10多年的時間序列,並且希望將xtick標籤縮寫爲2位數年份。 我該怎麼做?如何在matplotlib圖中縮寫xtick標籤年數至2位數

import matplotlib.pyplot as plt 
import datetime as dt 
import pandas as pd 
import pandas.io.data as web 

stocklist = ['MSFT'] 

# read historical prices for last 11 years 
def get_px(stock, start): 
    return web.get_data_yahoo(stock, start)['Adj Close'] 

today = dt.date.today() 
start = str(dt.date(today.year-11, today.month, today.day)) 
px = pd.DataFrame({n: get_px(n, start) for n in stocklist}) 
plt.plot(px.index, px[stocklist[0]]) 
plt.show() 
+1

+1良好的自包含例如 – tacaswell

回答

0

這條嵌入在可疑的方式pandas內部,但

ax = plt.gca() 
ax.get_xaxis().get_major_formatter().scaled[365] = '%y' 
plt.draw() 

format spec

+0

名 '斧頭' 沒有定義。我必須創建一個子圖來使用它嗎? – ronnydw

+0

@rdw對不起,請參閱編輯 – tacaswell

+0

太棒了!這樣做的工作。 – ronnydw