I have the following script to plot data from a csv file:
csv:
Date,% responding right,% responding wrong
02/08/16,46,42
09/08/16,45,44
...
21/08/18,41,47
29/08/18,42,47
04/09/18,42,48
script:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
df = pd.read_csv("brexit.csv")
df['Date'] = pd.to_datetime(df['Date'])
df.sort_values(by=['Date'])
plt.plot(df['Date'], df['% responding right'])
plt.plot(df['Date'], df['% responding wrong'])
plt.gcf().autofmt_xdate()
plt.savefig('brexit.png', dpi=300)
Result:
I tried to modify my script so the dates on the x axis are properly formatted, and the dates only show the year as it increases incrementally:
df = pd.read_csv("brexit.csv")
df['Date'] = pd.to_datetime(df['Date'])
df.sort_values(by=['Date'])
plt.plot(df['Date'], df['% responding right'])
plt.plot(df['Date'], df['% responding wrong'])
plt.gcf().autofmt_xdate()
plt.savefig('brexit.png', dpi=300)
But unfortunately this seems to mess up the order of my y values and still displays the full date instead of just the year:
How can I modify my code so the graph is displayed properly?