Reputation: 13
I tried to plot everyday high and low temperatures and to have x labels by each month. But I can only get Jan-Jul displayed in my plot. Could anyone help?
x = np.arange(0, 365, dtype=float)
x_ticks_labels = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
plt.plot(x, high_temp, '--', x, low_temp, '--')
ax.set_xticklabels(x_ticks_labels, rotation = 45)
Upvotes: 0
Views: 65
Reputation: 80279
When you set the xticklabels, it is recommended to also set the xticks. As the x-axis are the days from 0 to 364, the ticks should come on every first day of the month:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(0, 365)
low_temp = -5 + np.random.normal(0, .5, 365).cumsum() - 16 * np.cos(x / 365 * 2 * np.pi)
low_temp += np.linspace(0, low_temp[0] - low_temp[-1], 365) # make the values circular
high_temp = 20 + np.random.normal(0, .5, 365).cumsum() - 15 * np.cos(x / 365 * 2 * np.pi)
high_temp += np.linspace(0, high_temp[0] - high_temp[-1], 365) # make the values circular
x_ticks_labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
fig, ax = plt.subplots(figsize=(12, 5))
ax.plot(x, high_temp, '--', x, low_temp, '--')
ax.fill_between(x, low_temp, high_temp, color='lightblue', alpha=0.2)
ax.set_xticks(np.cumsum(days_in_month) - days_in_month[0])
ax.set_xticklabels(x_ticks_labels, rotation=0, ha='left')
ax.autoscale(enable=True, axis='x', tight=True)
plt.show()
Upvotes: 1