Your approach to converting the date string into a datatime
object is correct. It should normally contain as much information as possible.
You are then hoping to only use the day and month for plotting your time based data using matplotlib. The usual approach though is to first convert your dates into datatime
objects (which you have done), and to then get matplotlib to convert these values into its own internal representation using the date2num()
function.
Give this function the whole datetime
object including the year. If your data happened to span more than 12 months (or if it crossed from December to January) you would need it, even if you don't wish it to be displayed.
Next, you can tell matplotlib how to format any ticks on the x-axis using a formatter, in this case a DateFormatter()
. So you could for example choose to display month and day for each tick.
In this example, I display the month, with the day on the line below:
from matplotlib import pyplot, dates
from datetime import datetime
data = [("19800801", 16), ("19800810", 10), ("19800901", 15), ("19800905", 14)]
xaxis = [datetime.strptime(d, '%Y%m%d') for d, v in data]
yaxis = [v for d, v in data]
ax = pyplot.gca()
xaxis = dates.date2num(xaxis) # Convert to maplotlib format
hfmt = dates.DateFormatter('%m\n%d')
ax.xaxis.set_major_formatter(hfmt)
pyplot.xlabel('Date')
pyplot.ylabel('Value')
pyplot.plot(xaxis, yaxis)
pyplot.tight_layout()
pyplot.show()
This would display as follows:

You could then extend this to use a DayLocator()
which tells matplotlib to place the xticks at exact day locations as follows:
ax.xaxis.set_major_locator(dates.DayLocator())
Giving you:
