我一直在尝试使用格式为HH:MM:SS的x轴制作一个简单的图,其中的值取自相同格式的文件。我正在使用numpy.loadtxt和转换器来读取时间和值,如下所示:
time, moisture = np.loadtxt(dirlist[0], usecols=(1,2), unpack=True, converters = {1: dates.strpdate2num("%H:%M:%S")})
但是,当我使用date_plot绘图时,收到以下错误:
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
在我看来它抱怨失踪的一年时,我决定使用num2date
检查解析的日期,并返回1900-01-01 11:10:41+00:00
的正确日期,但我不知道为什么会有+00:00
。这可能是我困境的根源吗?
即使strpdate2num
正确解析,我决定检查是否在文件中的时间开头添加YYYY:MM:DD
它是否正常工作并且低并且看到它。不幸的是,我宁愿不在每个时间值的开头添加YYYY:MM:DD
,所以最后我的问题是:如何在没有YYYY:MM:DD
前缀的情况下阅读时如何使plot_date工作?为什么strpdate2num
是不是要照顾这个?
干杯, 戴夫
根据要求,输入文件的片段:
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
这是我的代码:
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()
答案 0 :(得分:0)
这似乎是一个奇怪的问题,因为您的原始代码看起来不错。这在Python 2中适用于我(Python 3需要从字节数据转换)。我用你的示例数据尝试了这个:
import matplotlib.pyplot as plt
from matplotlib.dates import strpdate2num
import numpy as np
time, val = np.loadtxt("File.txt", usecols=(1,2), unpack=True, converters = {1: strpdate2num("%H:%M:%S")})
plt.plot_date(time, val)
plt.show()