0

I'm analyzing coronavirus data in my country and I want to plot the data on new deaths, and total deaths in one plot. Initially I plotted it using the inbuilt plot function on the dataframe.

reports.plot(x='date', y='new_deaths')
reports.plot(x='date', y='total_deaths')
plt.xlabel('date')
plt.ylabel('cases')
plt.show()

Which yielded these images. enter image description here

enter image description here

However, I wanted to change the legends, and have them to be in one plot instead so I went ahead and used plt.subplot(111).

fig = plt.figure()
ax = plt.subplot(111)

ax.plot(ph_reports_april.date, ph_reports_april.new_deaths, label='New Deaths')
ax.plot(ph_reports_april.date, ph_reports_april.total_deaths, label='Total Deaths')
plt.xlabel('Date')
plt.ylabel('Cases')
ax.legend()
plt.show()

It got the job done except for one little problem. enter image description here

The dates are congested. Is there a way to have the date similar to how the dataframe.plot() function works? I've browse through this site and haven't found anything of value regarding on spacing out the values of date in the x-axis.

neil_ruaro
  • 368
  • 3
  • 15
  • what happens when you pass the `ax` to the dataframe plotting function `reports.plot(x='date', y='new_deaths', ax=ax)` and `reports.plot(x='date', y='total_deaths', ax=ax)`? – Stef Jan 12 '21 at 14:13
  • Set in matplotlib the Locator according to your needs. There are many examples on SO, lazily, [I will link to one of my own answers](https://stackoverflow.com/a/65343940/8881141). – Mr. T Jan 12 '21 at 21:00

0 Answers0