I want to shift the start position of the red line from "FEB-2020" to "JAN-2021". Currently this is my code and a picture of my current output. Basically Shorten the period of the whole graph to the dates stated above.
# plot daily vaccinated
fig, ax1= plt.subplots(1,figsize=(20,10))
# set up
plt.ticklabel_format(style='plain') #changing the tick figure from le 6 to millions
plt.setp(ax1, xticks= np.arange(0, 680, 30), xticklabels=vac_dates)
# plot chart
ax1.plot(vaccinated['received_at_least_one_dose'], label= 'Total Vaccinated', c='Red')
# axis and legend settings
ax1.set_ylabel('population (millions)', size= 14)
ax1.set_title('Monthly Vaccinated Numbers', size= 20)
plt.xticks(rotation=45)
plt.grid()
ax1.legend(loc="upper left")
##########################################################
# plot daily covid cases
ax2 = ax1.twinx()
# np.arrange looks at the number of rows
plt.setp(ax2, xticks= np.arange(0, 1035, 31), xticklabels=dates)
ax2.xaxis.tick_top()
ax2.plot(infected_update)
plt.xlabel('date', fontsize=14)
plt.ylabel('population (thousands)', fontsize=14)
plt.grid(False)
ax2.legend(['imported','local'], loc="upper right")
I've tried using codes from the following links but it doesn't seem to work
This means in your case, you have to make sure to set parse_dates=True
and set the index, when your read your data. Using pd.read_csv()
this could look like
df = pd.read_csv('covid.csv', sep=',', parse_dates=True, index_col=0)
Because you have to DataFrames, you have so make yure both have a DatetimeIndex. Afterwards just replace the columns in the two calls with ac.plot()
.
Comment:
If you want to plot all columns of a Dataframe, ax2.plot(df_2.index, df_2)
works. If your want to select a subset of columns ax2.plot(df_2.index, df_2[['b', 'c']])
is doing the job.