I have been trying to make a simple plot with an x-axis in the format HH:MM:SS with the values taken from a file in the same format. I'm using numpy.loadtxt and a converter to read the times and values as follows:
time, moisture = np.loadtxt(dirlist[0], usecols=(1,2), unpack=True, converters = {1: dates.strpdate2num("%H:%M:%S")})
However, when I plot using date_plot, I receive the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__
return self.func(*args)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 276, in resize
self.show()
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 348, in draw
FigureCanvasAgg.draw(self)
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_agg.py", line 451, in draw
self.figure.draw(self.renderer)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1034, in draw
func(*args)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axes.py", line 2086, in draw
a.draw(renderer)
File "C:\Python27\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 1091, in draw
ticks_to_draw = self._update_ticks(renderer)
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 945, in _update_ticks
tick_tups = [t for t in self.iter_ticks()]
File "C:\Python27\lib\site-packages\matplotlib\axis.py", line 893, in iter_ticks
for i, val in enumerate(majorLocs)]
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 524, in __call__
return self._formatter(x, pos)
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 375, in __call__
return self.strftime(dt, self.fmt)
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 413, in strftime
s1 = time.strftime(fmt, (year,) + timetuple[1:])
ValueError: Invalid format string
As it looks to me that its complaining about a missing year, I decided to check the parsed date using num2date
and the correct date of 1900-01-01 11:10:41+00:00
was returned although I don't know why there is an +00:00
. Could this be the source of my woes?
Even though strpdate2num
is parsing correctly, I decided to check if I add a YYYY:MM:DD
to the beginning of the times within the file would it work and low and behold it did. Unfortunately, I'd rather not append a YYYY:MM:DD
to the beginning of every time value so, finally my question is: how do I get plot_date to work while reading in a time without an YYYY:MM:DD
prefix and why is strpdate2num
not taking care of this?
Cheers, Dave
As requested, a snippet of the input file:
0.465752314814815 11:10:41 9.086096129
0.466226851851852 11:11:22 8.346124754
0.466701388888889 11:12:03 8.179795013
0.467175925925926 11:12:44 8.364891272
0.467662037037037 11:13:26 8.147598262
0.468136574074074 11:14:07 8.35810013
0.468611111111111 11:14:48 8.333816653
0.469085648148148 11:15:29 8.41123069
0.469085648148148 11:15:29 8.130661712
0.469560185185185 11:16:10 3.131862435
Here is my code:
datefunc = lambda x: dates.date2num(datetime.datetime.strptime(x, '%H:%M:%S'))
times, moisture = np.loadtxt(dirlist[0], delimiter='\t', usecols=(1,2), converters={1: datefunc}, unpack=True)
times_date=[]
for t in times:
times_date.append(dates.num2date(t)) #Converting back to a datetime object
plt.plot_date(times_date , moisture, xdate=True)
# plt.gcf().autofmt_xdate()
plt.show()
plt.close()
It seems like a strange problem as you're original code looks ok. This works for me in Python 2 (Python 3 requires conversion from byte data). I have tried this with your example data: