Grouping of x axis values

Question:

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.

Plot

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.

Asked By: Sourav

||

Answers:

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)

enter image description here

Answered By: Paul H
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.