我正在建立一个有两条线的线路图,一条用于高温,另一条用于低温。x轴基于日期时间格式(2014-01-01等)的一整年的天数。然而,我改变了标签,而不是数据,长达数月(一月、二月、三月等)。问题是第一个标签“Jan”在原产地。我想将所有标签向右移动,使它们在刻度之间居中。在
fig, ax = plt.subplots()
plt.plot(x, y1)
plt.plot(x, y2)
# Change x-axis from %y-%m-%d format to %m:
monthsFmt = mdates.DateFormatter('%m')
plt.gca().xaxis.set_major_formatter(monthsFmt)
# Replace numeric x-axis labels (1,2,3, ..., 12) for abbreviations of months ('Jan', 'Feb', 'Mar', etc.):
labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
ax.set_xticklabels(labels)
# CODE GOES HERE TO CENTER X-AXIS LABELS...
# Render plot:
plt.show()
这是我要寻找的结果:
在绘图时直接使用日期通常是有意义的。因此,可以使用
matplotlib.dates
定位器来代替手动设置标签的固定位置。如果要在月中定位蜱虫,可以选择该月的15日一个完整的例子:
^{pr2}$使用DavidG发布的帖子中建议的小记号应该可以。下面是一个MWE,我根据您的具体问题进行了调整,强制在每个月的第一天显示主要刻度,并使用小刻度将标签放在主要刻度之间:
结果是:
差不多,Jean-Sébastien。除了上个月,一切都很完美。它没有出现。在
你的解决方案做了一些调整:
有一个情节是这样的:
有什么线索可以让十二月加入x轴标签吗?总之,非常感谢。在
相关问题 更多 >
编程相关推荐