I have a code which allows me to plot two timeseries of data. I also have a number of events which I plot as a series of vertical lines within the timeseries. I have this timeseries for three years: 2015, 2016, and 2017. My code works well for 2015 and 2016, and I produce a graph like so (this is for 2016):
However, when I plot my data for 2017, I cannot reproduce the vertical lines for event data, as shown below (it's hard to see, but in this graph there are no vertical dashed lines as there were in first one):
Any ideas as to why my code does not work for 2017? The data are stored in .csv files, and event data is plotted from a column of data in the .csv file that I have converted into a series of datetime objects. I have checked, and the formats for the 2015, 2016 and 2017 .csv files remain the same.
FYI, here is the code that I have used to plot:
#Change format of "dates" column from string to datetime object
FMT = '%d/%m/%Y'
dtlist = []
for i in range(0, len(dates)):
dtlist.append(datetime.datetime.strptime(dates[i], FMT))
##EVENT:
#Remove all values where there is no event
df2 = df_17[pd.notnull(df_17['event'])]
#Convert event values to datetime objects
df2['event'] = pd.to_datetime(df2['event'], format="%d/%m/%Y")
event_list = df2['event'].tolist()
#Plot timeseries on graph
fig, ax1 = plt.subplots(figsize=(18, 6))
plt.plot(dtlist, series_1, color='b', label='Series 1')
ymin, ymax = ax1.get_ylim()
for value in event_list:
plt.axvline(x=value, ymin=ymin, ymax=ymax-1, color='k', linewidth=1.0, linestyle='--')
ax1.set_xlabel('Time')
ax1.set_ylabel('Series 1')
ax2=ax1.twinx()
ax2.plot(dtlist, series_2, color='r', label='Series_2')
ax2.set_ylabel('Series 2')
plt.legend
plt.show()
Any help much appreciated!