绘图中DateTimeIndex的格式设置
我很难用pandas破解更改勾号频率和日期格式的文档 例如:绘图中DateTimeIndex的格式设置,date,numpy,pandas,matplotlib,format,Date,Numpy,Pandas,Matplotlib,Format,我很难用pandas破解更改勾号频率和日期格式的文档 例如: 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
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')
生成此绘图:
哎呀。我怎样才能让滴答声在每个月、每个季度或其他合理的时间出现?如何更改日期格式以消除时间
我用
ax.set\u xticks()
和ax.xaxis.set\u major\u formatter
尝试了各种方法,但都没有找到答案。如果使用pandas
中的plot
方法,那么matplotlib
的set\u major\u locator
和set\u major\u formatter
方法很可能会失败。如果您想继续使用pandas``plot
方法,手动调整刻度可能更容易
#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])
可能重复感谢!根据这个答案,我写了@Dror,你应该把它作为一个正确的答案,因为它包含了额外的信息。