日期时间表的格式化 [英] Formatting of DateTimeIndex in plot pandas
问题描述
例如:
$ b $我无法解密文件,用于更改蝙蝠频率和日期格式的大熊猫。 b
import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib as mpl
% matplotlib inline
mpl.style.use('ggplot')
mpl.rcParams ['figure.figsize'] =(8,6)
#grab一些价格数据
px = web.DataReader('AAPL',yahoo,'2010-12-01')['Adj Close']
px_m = px.asfreq('M' ='ffill')
rets_m = px_m.pct_change()
rets_m.plot(kind ='bar')
生成此图:
Yikes。如何让每一个季度或每季度有所???日期格式如何改变以摆脱时间?
我已经尝试过各种各样的东西与 ax.set_xticks()
和 ax.xaxis.set_major_formatter
,但无法弄清楚。
如果您在 pandas
中使用 plot
方法, set_major_locator
和 set_major_formatter
方法 matplotlib
很可能
I'm having trouble deciphering the documentation for changing tick frequency and date formatting with pandas.
For example:
import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib as mpl
%matplotlib inline
mpl.style.use('ggplot')
mpl.rcParams['figure.figsize'] = (8,6)
# grab some price data
px = web.DataReader('AAPL', "yahoo", '2010-12-01')['Adj Close']
px_m = px.asfreq('M', method='ffill')
rets_m = px_m.pct_change()
rets_m.plot(kind='bar')
generates this plot:
Yikes. How can I get the ticks to be every month or quarter or something sensible? And how can the date formatting be changed to get rid of times?
I've tried various things with ax.set_xticks()
and ax.xaxis.set_major_formatter
but haven't been able to figure it out.
If you use the plot
method in pandas
, the set_major_locator
and set_major_formatter
methods of matplotlib
is likely to fail. It might just be easier to manually adjust the ticks, if you want to stay with pandas``plot
methods.
#True if it is the first month of a quarter, False otherwise
xtick_idx = np.hstack((True,
np.diff(rets_m.index.quarter)!=0))
#Year-Quarter string for the tick labels.
xtick = ['{0:d} quarter {1:d}'.format(*item)
for item in zip(rets_m.index.year, rets_m.index.quarter)]
ax =rets_m.plot(kind='bar')
#Only put ticks on the 1st months of each quarter
ax.xaxis.set_ticks(np.arange(len(xtick))[xtick_idx])
#Adjust the ticklabels
ax.xaxis.set_ticklabels(np.array(xtick)[xtick_idx])
这篇关于日期时间表的格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!