Home > Enterprise >  How to show month on x axis for only 12 data points
How to show month on x axis for only 12 data points

Time:10-16

I am able to create a plot with the name of the month when I have 365 data points with the following code:

y = np.random.normal(size=365)
x = np.array(range(len(y)))
plt.plot(x, y)
plt.xlabel('Month')
locator = mdates.MonthLocator()
fmt = mdates.DateFormatter('%b')
X = plt.gca().xaxis
X.set_major_locator(locator)
X.set_major_formatter(fmt)

Here is the result, which is exactly what I'm looking for: enter image description here

I would like to do the same thing but with only 12 data points (one for each month). If I just change the 365 to 12 (y = np.random.normal(size=12)), it looks like this:

enter image description here

How can I get it to show all the months in the x axis as in the first graph?

I tried passing arguments to enter image description here

  • Related