4

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

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, thay you very much! I thought when I imported the package it would import the whole thing
@AugustoDiasNoronha: Nested packages need to be imported explicitly. Sometimes a parent package does this for you, but not in this case.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.