Python Matplotlib X轴值分组 [英] Python matplotlib grouping of x axis values

查看:88
本文介绍了Python Matplotlib X轴值分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下图表(使用matplotlib绘制).如您在x轴上看到的,一年中的天以0-364之间的数字显示.

I have the following plot (plotted using matplotlib). As you can see in x-axis, the days of year are shown as numbers from 0-364.

但是我想将x轴分成12个相等的部分.输出结果必须显示月份的名称,如Jan,Feb,Mar,...,Nov,Dec.

But I want to partition the x-axis in 12 equal parts. The output must show the names of the months as Jan, Feb, Mar,..., Nov, Dec. Is there any known way to achieve that on matplotlib.

推荐答案

我认为您只想使用月份定位符来绘制日期.

I think you just want to plot dates with a month locator.

%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)

这篇关于Python Matplotlib X轴值分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆