Pandas timeseries绘制设置x轴主要和次要刻度和标签

Pandas timeseries绘制设置x轴主要和次要刻度和标签

我希望能够为从Pandas时间序列对象绘制的时间序列图设置主要和次要xticks及其标签。

熊猫0.9“什么是新的”页面说:

“您可以使用to_pydatetime或为Timestamp类型注册转换器”

但我无法弄清楚如何做到这一点,以便我可以使用matplotlib ax.xaxis.set_major_locatorax.xaxis.set_major_formatter(和次要)命令。

如果我在不转换熊猫时间的情况下使用它们,则x轴刻度和标签最终会出错。

通过使用'xticks'参数,我可以将主刻度传递给pandas.plot,然后设置主刻度标签。我无法弄清楚如何使用这种方法进行次要滴答。(我可以在pandas.plot设置的默认次要刻度上设置标签)

这是我的测试代码:

import pandasprint 'pandas.__version__ is ', pandas.__version__print 'matplotlib.__version__ is ', matplotlib.__version__    

dStart = datetime.datetime(2011,5,1) # 1 MaydEnd = 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 indexax.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, which="minor")ax.xaxis.grid(False, which="major")ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('\n\n\n%b%Y'))plt.show()    # set the major xticks and labels through pandasax2 = plt.figure(figsize=(7,4), dpi=300).add_subplot(111)xticks = pandas.date_range(start=dStart, end=dEnd, freq='W-Tue')print "xticks: ", xticks
testSeries.plot(ax=ax2, style='-v', label='second line',
梵蒂冈之花
浏览 1978回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python