Python 如何更改图形标签的日期类型
您可以查看[7]->日期类型Python 如何更改图形标签的日期类型,python,date,pandas,matplotlib,label,Python,Date,Pandas,Matplotlib,Label,您可以查看[7]->日期类型2015.01.02 但是 您可以在图形上看到out[17]->x轴仅显示0、50、100 如何更改x轴如2015.01.02 有什么建议吗?您可以尝试先转换列日期,然后和最后更改轴x的日期时间格式: 按注释编辑: 这看起来像一个bug,但您可以更改索引,然后旋转标签: df['date'] = pd.to_datetime(df['date'] ) #print df a = df['a'].pct_change() b = df['b'].pct_chang
2015.01.02
但是
您可以在图形上看到out[17]
->x轴
仅显示0、50、100
如何更改x轴
如2015.01.02
有什么建议吗?您可以尝试先转换列
日期
,然后和最后更改轴x
的日期时间格式:
按注释编辑:
这看起来像一个bug,但您可以更改索引
,然后旋转标签
:
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.strftime(“%Y.%m.%d”)
thx设置的。那么,我如何更改涵盖整个期间的日期范围……从2015-01-02到2015-01-13
ax = df['corr'].plot()
ticklabels = df.index.strftime('%Y.%m.%d')
ax.xaxis.set_major_formatter(ticker.FixedFormatter(ticklabels))
plt.show()
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()