'Matplotlib and datetime error date2num function AttributeError

I'm trying to plot a graph with a list of datetime objects as one axis. I searched online and it seems I should call the date2num function. However, when I call it I get an Attribute error.

Here's the code I wrote:

listOfDates
[datetime.date(2013, 8, 20), datetime.date(2013, 8, 21)]
dates = mathplotlib.dates.date2num(listOfDates)

Here's the error I get:

Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
  dates = matplotlib.dates.date2num(listOfDates)
AttributeError: 'module' object has no attribute 'dates'

Thank you very much



Solution 1:[1]

You need to import the matplotlib.dates module explicitly:

import matplotlib.dates

before it is available.

Alternatively, import the function into your local namespace:

from matplotlib.dates import date2num

dates = date2num(listOfDates)

Solution 2:[2]

this error usually comes up when the date and time of your system is not correct. just correct and restart the whole console and it should work perfect.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martijn Pieters
Solution 2 Nikhil Kumar