我有HH:MM:SS格式的时间戳列表,并希望使用datetime.time对某些值进行绘图。好像python不喜欢我这样做的方式。有人可以帮忙吗?
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*
答案 0 :(得分:0)
从datetime
到matplotlib
约定兼容 float
的日期/时间
像往常一样, devil 会被隐藏起来。
matplotlib
日期 几乎 相等,但不等于:
# mPlotDATEs.date2num.__doc__
#
# *d* is either a class `datetime` instance or a sequence of datetimes.
#
# Return value is a floating point number (or sequence of floats)
# which gives the number of days (fraction part represents hours,
# minutes, seconds) since 0001-01-01 00:00:00 UTC, *plus* *one*.
# The addition of one here is a historical artifact. Also, note
# that the Gregorian calendar is assumed; this is not universal
# practice. For details, see the module docstring.
因此,强烈建议重新使用他们自己的"工具:
from matplotlib import dates as mPlotDATEs # helper functions num2date()
# # and date2num()
# # to convert to/from.
matplotlib
也为这部分带来了武器。
答案 1 :(得分:0)
它仍然是Python 3.5.3和Matplotlib 2.1.0中的有效问题。
解决方法是使用datetime.datetime
个对象而不是datetime.time
个对象:
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:
import matplotlib.dates as mdates
ax.xaxis.set_major_formatter(mdates.DateFormatter('%H-%M-%S'))