matplotlib:将时间序列图中周期数据下的周期标签居中
在绘制和有效标记密集时间序列数据时,matplotlib 的轴格式化选项往往会表现不佳。
一个问题是刻度标签与刻度相关联,因此如果您以适当的频率设置轴刻度,通常会出现太多标签。这也意味着,如果您要绘制几年期间的每日数据,则没有好的方法可以在每年的自然位置上标记 x 轴:在年份数据下方居中(即在 x 轴下方) - 7 月 2 日或附近的轴位置)。
此示例中描述的技巧 - 将主要刻度设置在您想要的位置,然后使用不可见的次要刻度来放置其他地方的标签 - 有效,但它限制您只能看到一组可见的轴刻度(因为每个轴仅限于一组主要刻度和一组次要刻度)。例如,您无法在每年年初显示主要刻度,在每个月初显示次要刻度,而不放弃将年份标签在主要(每年)刻度之间适当居中的能力,正如您在出版物中看到的那样-优质地块。
是否有一种解决方法,不需要完全手动绘制所有内容?
matplotlib's axis-formatting options tend to fall flat when it comes to plotting and effectively labeling dense time-series data.
One problem is that tick labels are tied to ticks, so if you set axis ticks at an appropriate frequency, there are usually too many labels. This also means that if you are plotting, say, daily data over a period of several years, there is no good way to label the x-axis with each year in its natural position: centered under the year's data (i.e., under the x-axis position for July 2, or thereabouts).
The trick described in this example—set major ticks where you want them, then use invisible minor ticks to place the labels elsewhere—works, but it limits you to one visible set of axis ticks (since each axis is limited to one set of major and one set of minor ticks). You can't show, say, major ticks at the start of each year and minor ticks at the start of each month without giving up the ability to put year labels centered appropriately between the major (yearly) ticks, as you would find in publication-quality plots.
Is there a work-around that doesn't involve drawing everything fully manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否看过 scikits.timeseries 中的 tsplot 功能?最近维护得不多,但效果还不错。我将在不久的将来的某个时候将该代码移植到 pandas 中。
Have you looked at the tsplot capability in scikits.timeseries? It hasn't been maintained much recently, but it works pretty well. I'll be porting that code into pandas at some point in the relative near future.