python - 使用 matplotlib 从纪元开始绘制 time() 的日期

标签 python matplotlib

我将自纪元以来的第二个日期作为 float (来自 time.time() ),我试图将其绘制成这样(行[0]):

x,y = [],[]
csv_reader = csv.reader(open(csvFile))
for line in csv_reader:
    x.append(float(line[1]))
    y.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(line[0]))))


fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'o-')
fig.autofmt_xdate()

plt.show()

但是 matplotlib 一直这样出错:

ValueError: invalid literal for float(): 2013-07-08 15:04:50

关于如何正确格式化它的任何想法?

干杯

最佳答案

您收到 ValueError,因为该格式对 float() 无效。不要将其格式化为 float 值,而是尝试将格式化的字符串附加到列表中并使用 yticks() 函数,如下所示。

>>> yAxis = ['2013-07-08 15:04:50', '2013-07-08 15:03:50', '2013-07-08 5:04:50']
>>> from random import randint
>>> xAxis = [randint(0, 10) for _ in range(3)]
>>> import pylab as plt
>>> plt.plot(xAxis, range(len(xAxis)))
>>> plt.yticks(range(len(yAxis)), yAxis, size='small')
>>> plt.show()

这给你一个像下面这样的情节,希望这就是你要找的:

enter image description here

P.S - 您想要 X 轴上的日期吗?

关于python - 使用 matplotlib 从纪元开始绘制 time() 的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17535065/

相关文章:

python - 将 x 的函数作为 x 刻度标签

python - matplotlib Axes3D 中的 mayavi 3d 对象

python - Pandas 和 Matplotlib 将 df 绘制为具有 2 个 y 轴的子图

python - 如何使用 pyodbc 获取 SQL Server 存储过程的返回值?

python - NameError:名称 'now' 未定义

python - 是否有一个开关可以忽略 LXML 中未定义的 namespace 前缀?

python - 更一致的 Sphinx 主题

python - 聚类经纬度gps数据

python - 从图中删除颜色条

python - 如何使用 Python 绘制能量排名图?