How do I change the number of ticks?

So, I've searched in SE for a while, and I don't find anything that works ..... I have a timeline of the minimum and maximum temperatures over the years.

plt.figure()
plt.gca().fill_between(range(len(Tmin)), Tmin, Tmax, facecolor='blue', alpha=0.25)
plt.plot(range(365), Tmin, 'b', range(365), Tmax, 'r')
plt.locator_params(axis = 'x', nbins = 12)

x = plt.gca().xaxis
m = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
ax = plt.gca()
ax.locator_params(axis = 'x', nbins = 12)
ax.set_xticklabels(m)
ax.set_ylabel('T(C)')
ax.set_title('Temperature')
plt.xlim([0,365])
# ax.set_xticks(np.linspace(0,1,12))
plt.show()

      

It still displays the same number of ticks as on the original chart, where the x-axis is [0, 50, 100, ..., 350]

enter image description here

+3


source to share


1 answer


You can manually check the boxes to get the days from January 1st for each month.

import datetime as dt
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(365)
y = 100 + 250*np.sin(9*(x-50)/720) + 80*np.random.rand(365)

m = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
# get the start day for each month as an index
ticks = [(dt.date(2016,m,1)-d0).days for m in range(1,13)]

fig,ax = plt.subplots(1,1)
ax.plot(x,y)
ax.set_xticks(ticks)
ax.set_xticklabels(m)
ax.set_xlim(-3,365)
plt.show()

      



enter image description here

+2


source







All Articles