python - x 轴值的分组

标签 python python-3.x matplotlib

<分区>

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

Plot

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

最佳答案

我认为您只想使用月份定位器绘制日期。

%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

关于python - x 轴值的分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42866289/

相关文章:

python - 如何在 Django 中正确使用 auto_created 属性?

python - 为什么不能在 f 字符串中使用 "await"?

python - 修改包装函数中的函数代码

python - SWIG 一般问题

python - tornadoweb 的配置失败 nginx 设置,未知指令 "user"

python - 从 SOAP 响应中解析多个值

python - seaborn plot_marginals 多个 kdeplots

python - 更改等高线标签文本对象的方向

python - 如何修复 ImportError : No module named numpy?

python - 使用 Django 将查询集组合成单个字符串