如何更改图表标签的日期类型
How can I change date type for graph labels
可以看到out[7]
-> 日期类型2015.01.02
但
您可以在图表上看到 out[17]
-> x-axis
仅显示 0, 50, 100
如何将 x-axis
更改为 2015.01.02
请问有什么建议吗?
您可以尝试先转换列 date
to_datetime
, then set_index
and last change datetime format of axis
x
by strftime
and 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()
可以看到out[7]
-> 日期类型2015.01.02
但
您可以在图表上看到 out[17]
-> x-axis
仅显示 0, 50, 100
如何将 x-axis
更改为 2015.01.02
请问有什么建议吗?
您可以尝试先转换列 date
to_datetime
, then set_index
and last change datetime format of axis
x
by strftime
and 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()