X-Label on chart

Under the Guided Project: Storytelling Data Visualization on Exchange Rates

Mission Link: Learn data science with Python and R projects

image

My Code:

non_covid = euro_to_dollar[euro_to_dollar[‘Time’].dt.year > 2016]
plt.plot(non_covid[‘Time’],non_covid[‘rolling_mean’])
plt.show()

Replace this line with your code


What I expected to happen:

X-Label showing Year only

What actually happened: 

X-Label showing Year + Month

I was interested in solving your problem and found this article which helped me create the code below:

import matplotlib.dates as mdates
euro_to_dollar['rolling_mean'] = euro_to_dollar['US_dollar'].rolling(30).mean()
non_covid = euro_to_dollar[euro_to_dollar['Time'].dt.year > 2016]
fig, ax = plt.subplots()
ax.plot(non_covid['Time'],non_covid['rolling_mean'])
ax.xaxis.set_tick_params(reset=True)
ax.xaxis.set_major_locator(mdates.YearLocator(1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()

This block of code produced this plot:
image

Is this your desired result?

thanks so much. exactly what I expected

1 Like