我希望能够为从Pandas时间序列对象绘制的时间序列图设置主要和次要xticks及其标签.
熊猫0.9"什么是新的"页面说:
"您可以使用to_pydatetime或为Timestamp类型注册转换器"
但我无法弄清楚如何做到这一点,以便我可以使用matplotlib ax.xaxis.set_major_locator和ax.xaxis.set_major_formatter(和次要)命令.
如果我在不转换熊猫时间的情况下使用它们,则x轴刻度和标签最终会出错.
通过使用'xticks'参数,我可以将主刻度传递给pandas.plot,然后设置主刻度标签.我无法弄清楚如何使用这种方法进行次要滴答.(我可以在pandas.plot设置的默认次要刻度上设置标签)
这是我的测试代码:
import pandas
print 'pandas.__version__ is ', pandas.__version__
print 'matplotlib.__version__ is ', matplotlib.__version__
dStart = datetime.datetime(2011,5,1) # 1 May
dEnd = datetime.datetime(2011,7,1) # 1 July
dateIndex = pandas.date_range(start=dStart, end=dEnd, freq='D')
print "1 May to 1 July 2011", dateIndex
testSeries = pandas.Series(data=np.random.randn(len(dateIndex)),
index=dateIndex)
ax = plt.figure(figsize=(7,4), dpi=300).add_subplot(111)
testSeries.plot(ax=ax, style='v-', label='first line')
# using MatPlotLib date time locators and formatters doesn't work with new
# pandas datetime index
ax.xaxis.set_minor_locator(matplotlib.dates.WeekdayLocator(byweekday=(1),
interval=1))
ax.xaxis.set_minor_formatter(matplotlib.dates.DateFormatter('%d\n%a'))
ax.xaxis.grid(True, …Run Code Online (Sandbox Code Playgroud)