Python matplotlib分组的x轴值

问题描述:

我有以下情节(绘制使用matplotlib)。正如您在x轴中所看到的那样,一年中的日期显示为0-364之间的数字。Python matplotlib分组的x轴值

Plot

但我想在12个等份分割x轴。输出必须显示月份的名称,如Jan,Feb,Mar,...,Nov,Dec。有什么已知的方法可以在matplotlib上实现。

+0

有一种已知的方式,例如参见[这里](http://matplotlib.org/examples/pylab_examples/date_demo2.html)。顺便说一下,您可以在轴标签中引入LaTeX代码以获得更好的箭头,例如'plt.xlabel(r'Days $ \ rightarrow $')'。同样适用于图例,标题等。字符串前面的“r”代表原始字符串。 – Michael

+0

这个链接你给了帮助。我能够将X轴分成12个相等的部分。但我该如何将整数值从0到364转换为几个月。 – Sourav

+0

我试过了,就像你可以看到[这里](http://textuploader.com/dtxtw),尽管这可能不是最好的解决方案。对不起,我不知道如何在Stack Overflow的注释中正确地发布代码。我希望你了解代码的想法,所以你可以根据你的问题进行调整。 – Michael

我认为你只是想绘制一个月的定位器的日期。

%matplotlib inline 
from datetime import datetime 
import numpy 
from matplotlib import pyplot, dates, ticker 

# setup your date values (arbitrary year) 
oneday = datetime(1990, 1, 2) - datetime(1990, 1, 1) 
start = datetime(1990, 1, 1) 
end = datetime(1991, 1, 1) 
date = dates.drange(start, end, oneday) 

# make data 
values = numpy.random.uniform(low=-5, high=10, size=365).cumsum() 

# axis tooling 
locator = dates.MonthLocator() 
formatter = dates.DateFormatter('%b') 

# plot things, use the locators 
fig, ax = pyplot.subplots() 
ax.plot_date(date, values, '-') 
ax.xaxis.set_major_locator(locator) 
ax.xaxis.set_major_formatter(formatter) 

enter image description here

+0

感谢您的解决方案。有效 :) – Sourav