问题描述:

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!

网友答案:
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

相关阅读:
Top