你可以看到out[7]
- >最新型2015.01.02
但 你可以看到Øut[17]
- >x-axis
上圖只顯示0, 50, 100
我怎樣才能改變x-axis
like 2015.01.02
對此有何建議?
你可以看到out[7]
- >最新型2015.01.02
但 你可以看到Øut[17]
- >x-axis
上圖只顯示0, 50, 100
我怎樣才能改變x-axis
like 2015.01.02
對此有何建議?
你可以嘗試先轉換柱date
to_datetime
,然後set_index
和axis
x
最後更改日期時間格式由strftime
和set_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()
的評論編輯:
它似乎是錯誤的,但你可以改變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()
我很感激這個^^ –
另一個問題?爲什麼x軸只顯示2015.01.02〜2015.01.09 ??? –
Becouse它由'df.index.strftime('%Y.%m。%d')' – jezrael
你可以添加數據框和代碼示例文本,而不是圖片? – jezrael
歡迎來到Stack Overflow。你可以檢查[tour](http://stackoverflow.com/tour) – jezrael
當我看着這些圖片時,我的眼睛受到傷害 –