如何使用 datetime.time 在 Python 中绘图 [英] How to use datetime.time to plot in Python
本文介绍了如何使用 datetime.time 在 Python 中绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 HH:MM:SS 格式的时间戳列表,并想使用 datetime.time 绘制某些值.似乎python不喜欢我这样做的方式.有人可以帮忙吗?
I have list of timestamps in the format of HH:MM:SS and want to plot against some values using datetime.time. Seems like python doesn't like the way I do it. Can someone please help ?
import datetime
import matplotlib.pyplot as plt
# random data
x = [datetime.time(12,10,10), datetime.time(12, 11, 10)]
y = [1,5]
# plot
plt.plot(x,y)
plt.show()
*TypeError: float() argument must be a string or a number*
推荐答案
它在 Python 3.5.3 和 Matplotlib 2.1.0 中仍然有效.
It is still valid issue in Python 3.5.3 and Matplotlib 2.1.0.
解决方法是使用 datetime.datetime
对象而不是 datetime.time
对象:
A workaround is to use datetime.datetime
objects instead of datetime.time
ones:
import datetime
import matplotlib.pyplot as plt
# random data
x = [datetime.time(12,10,10), datetime.time(12, 11, 10)]
x_dt = [datetime.datetime.combine(datetime.date.today(), t) for t in x]
y = [1,5]
# plot
plt.plot(x_dt, y)
plt.show()
按默认日期部分不应该是可见的.否则你总是可以使用 DateFormatter:
By deafult date part should not be visible. Otherwise you can always use DateFormatter:
import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H-%M-%S'))
这篇关于如何使用 datetime.time 在 Python 中绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文