0

I've plotted a line, and I wanted to show every month from the beginning of the data to the end. For that, I've tried to use the date column given in the dataset, but values overlapped. Then I've tried the following code:


rollingus = united_states['new_cases'].rolling(7).mean()

fig,ax = plt.subplots(figsize=(12,9))
plt.plot(rollingus,label='7 Days rolling average')
plt.plot(united_states['new_cases'],label=' Number of new cases each day')
plt.xlabel('Date')
plt.ylabel('Number of new  cases')
plt.title('New cases In United States (Figure 1.1)')
ax.xaxis.set_major_locator(MultipleLocator(30))
ax.xaxis.set_minor_locator(MultipleLocator(7))
ax.set_xticklabels(['Dec','Jan','Feb 2020',' Mar',' Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec','Jan','Feb 2021','Mar','Apr'],minor=False)

plt.legend()
plt.show()

1

However, the dates are not exactly right. As shown in the figure I wanted them to start at the beginning of the y-values but it starts few values after that. Can anyone help me?

tmdavison
  • 64,360
  • 12
  • 187
  • 165
Atilla Colak
  • 133
  • 5
  • are you looking for [Changing the formatting of a datetime axis in matplotlib](https://stackoverflow.com/questions/43968985/changing-the-formatting-of-a-datetime-axis-in-matplotlib)? – FObersteiner Apr 08 '21 at 15:37
  • No, but I've tried plt.margin(y=0) and it seems to be working. – Atilla Colak Apr 08 '21 at 16:11

0 Answers0