2016-02-03 56 views
-1

enter image description here如何更改日期類型的圖形標記

你可以看到out[7] - >最新型2015.01.02 但 你可以看到Øut[17] - >x-axis上圖只顯示0, 50, 100

我怎樣才能改變x-axis like 2015.01.02

對此有何建議?

+3

你可以添加數據框和代碼示例文本,而不是圖片? – jezrael

+0

歡迎來到Stack Overflow。你可以檢查[tour](http://stackoverflow.com/tour) – jezrael

+1

當我看着這些圖片時,我的眼睛受到傷害 –

回答

1

你可以嘗試先轉換柱dateto_datetime,然後set_indexaxisx最後更改日期時間格式由strftimeset_major_formatter

import matplotlib.pyplot as plt 
import matplotlib.ticker as ticker 

df['date'] = pd.to_datetime(df['date']) 
print df 
     date a b c 
0 2015-01-02 10 20 3 
1 2015-01-05 40 50 6 
2 2015-01-06 70 80 8 
3 2015-01-07 80 50 9 
4 2015-01-08 90 50 3 
5 2015-01-09 10 20 3 
6 2015-01-10 40 50 6 
7 2015-01-11 70 80 8 
8 2015-01-12 80 50 9 
9 2015-01-13 90 50 3 

a = df['a'].pct_change() 
b = df['b'].pct_change() 
c = df['c'].pct_change() 
df['corr'] = pd.rolling_corr(a,b,3) 
df = df.set_index('date') 
print df 
      a b c  corr 
date       
2015-01-02 10 20 3  NaN 
2015-01-05 40 50 6  NaN 
2015-01-06 70 80 8  NaN 
2015-01-07 80 50 9 0.941542 
2015-01-08 90 50 3 0.914615 
2015-01-09 10 20 3 0.776273 
2015-01-10 40 50 6 0.999635 
2015-01-11 70 80 8 0.985112 
2015-01-12 80 50 9 0.941542 
2015-01-13 90 50 3 0.914615 
ax = df['corr'].plot() 

ticklabels = df.index.strftime('%Y.%m.%d') 
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels)) 
plt.show() 

graph_corr

的評論編輯:
它似乎是錯誤的,但你可以改變index然後旋轉labels

df['date'] = pd.to_datetime(df['date']) 
#print df 


a = df['a'].pct_change() 
b = df['b'].pct_change() 
c = df['c'].pct_change() 
df['corr'] = pd.rolling_corr(a,b,3) 

df = df.set_index('date') 
print df 
      a b c  corr 
date       
2015-01-02 10 20 3  NaN 
2015-01-05 40 50 6  NaN 
2015-01-06 70 80 8  NaN 
2015-01-07 80 50 9 0.941542 
2015-01-08 90 50 3 0.914615 
2015-01-09 10 20 3 0.776273 
2015-01-10 40 50 6 0.999635 
2015-01-11 70 80 8 0.985112 
2015-01-12 80 50 9 0.941542 
2015-01-13 90 50 3 0.914615 
df.index = df.index.strftime('%Y.%m.%d') 
print df 
      a b c  corr 
2015.01.02 10 20 3  NaN 
2015.01.05 40 50 6  NaN 
2015.01.06 70 80 8  NaN 
2015.01.07 80 50 9 0.941542 
2015.01.08 90 50 3 0.914615 
2015.01.09 10 20 3 0.776273 
2015.01.10 40 50 6 0.999635 
2015.01.11 70 80 8 0.985112 
2015.01.12 80 50 9 0.941542 
2015.01.13 90 50 3 0.914615 

ax = df['corr'].plot() 

plt.xticks(rotation=90) 
plt.show() 

graph

+0

我很感激這個^^ –

+0

另一個問題?爲什麼x軸只顯示2015.01.02〜2015.01.09 ??? –

+0

Becouse它由'df.index.strftime('%Y.%m。%d')' – jezrael

相關問題