0

I have an array of datetime objects that is the following

dates = [datetime.datetime(1900, 1, 1, 10, 8, 14, 565000), datetime.datetime(1900, 1, 1, 10, 8, 35, 330000), datetime.datetime(1900, 1, 1, 10, 8, 43, 358000), datetime.datetime(1900, 1, 1, 10, 8, 52, 808000)]

I then tried converting the array to matplotlib suitable objects using dates = plt.dates.date2num(dates)

Then I tried to plot it against some values using ax1.plot_date(dates, datac)

but received errors as follows:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:\Python34\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 278, in resize
    self.show()
  File "C:\Python34\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 349, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Python34\lib\site-packages\matplotlib\backends\backend_agg.py", line 461, in draw
    self.figure.draw(self.renderer)
  File "C:\Python34\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python34\lib\site-packages\matplotlib\figure.py", line 1079, in draw
    func(*args)
  File "C:\Python34\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 2092, in draw
    a.draw(renderer)
  File "C:\Python34\lib\site-packages\matplotlib\artist.py", line 59, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\Python34\lib\site-packages\matplotlib\axis.py", line 1103, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Python34\lib\site-packages\matplotlib\axis.py", line 957, in _update_ticks
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python34\lib\site-packages\matplotlib\axis.py", line 957, in <listcomp>
    tick_tups = [t for t in self.iter_ticks()]
  File "C:\Python34\lib\site-packages\matplotlib\axis.py", line 905, in iter_ticks
    for i, val in enumerate(majorLocs)]
  File "C:\Python34\lib\site-packages\matplotlib\axis.py", line 905, in <listcomp>
    for i, val in enumerate(majorLocs)]
  File "C:\Python34\lib\site-packages\matplotlib\dates.py", line 580, in __call__
    result = self._formatter(x, pos)
  File "C:\Python34\lib\site-packages\matplotlib\dates.py", line 412, in __call__
    return self.strftime(dt, self.fmt)
  File "C:\Python34\lib\site-packages\matplotlib\dates.py", line 450, in strftime
    s1 = time.strftime(fmt, (year,) + timetuple[1:])
ValueError: Invalid format string

Does anyone have any advice on how to fix this? Thanks in advance!

2
  • Supplying more complete code would help. When I guess at what you're doing I don't get any errors. Commented Oct 10, 2014 at 19:33
  • 2
    Just plot the datetime object directly. Commented Oct 10, 2014 at 21:34

1 Answer 1

1
dates = plt.dates.date2num(dates)

is not appropriate as you would have used the following command

import matplotlib.pyplot as plt

So here plt does not contain date2num function. So you would have to use

from matplotlib.dates import date2num

and dates =date2num(dates). I think this will work fine

Sign up to request clarification or add additional context in comments.

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.