Python matplotlib分组x轴值

时间:2017-03-17 20:08:57

标签: python python-3.x matplotlib

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

Plot

但是我想将x轴分成12个相等的部分。输出必须显示月份的名称为Jan,Feb,Mar,...,Nov,Dec。是否有任何已知的方法可以在matplotlib上实现。

1 个答案:

答案 0 :(得分:1)

我想你只想用月定位器绘制日期。

%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