【发布时间】:2015-03-12 15:24:06
【问题描述】:
我的数据显示了在三个不同日期收集的一些值:2015-01-08、2015-01-09 和 2015-01-12。对于每个日期,都有几个具有时间戳的数据点。
日期/时间在列表中,如下所示:
['2015-01-08-09:00:00', '2015-01-08-10:00:00', '2015-01-08-11:00:00', '2015-01-08-12:00:00', '2015-01-08-13:00:00', '2015-01-09-14:00:00', '2015-01-09-15:00:00', '2015-01-09-16:00:00', '2015-01-12-09:00:00', '2015-01-12-10:00:00', '2015-01-12-11:00:00']
另一方面,我在另一个列表中有相应的值(浮点数):
[12210.0, 12210.0, 12180.0, 12240.0, 12250.0, 12420.0, 12390.0, 12400.0, 12380.0, 12450.0, 12460.0]
为了将所有这些放在一起并绘制图表,我使用以下代码:
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as md
import dateutil
from matplotlib.font_manager import FontProperties
timestamps = ['2015-01-08-09:00:00', '2015-01-08-10:00:00', '2015-01-08-11:00:00', '2015-01-08-12:00:00', '2015-01-08-13:00:00', '2015-01-09-14:00:00', '2015-01-09-15:00:00', '2015-01-09-16:00:00', '2015-01-12-09:00:00', '2015-01-12-10:00:00', '2015-01-12-11:00:00']
ticks = [12210.0, 12210.0, 12180.0, 12240.0, 12250.0, 12420.0, 12390.0, 12400.0, 12380.0, 12450.0, 12460.0]
plt.subplots_adjust(bottom=0.2)
plt.xticks( rotation=90 )
dates = [dateutil.parser.parse(s) for s in timestamps]
ax=plt.gca()
ax.set_xticks(dates)
ax.tick_params(axis='x', labelsize=8)
xfmt = md.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(xfmt)
plt.plot(dates, ticks, label="Price")
plt.xlabel("Date and time", fontsize=12)
plt.ylabel("Price", fontsize=12)
plt.suptitle("Price during last three days", fontsize=12)
plt.legend(loc=0,prop={'size':8})
plt.savefig("figure.pdf")
当我尝试绘制这些日期时间和值时,我得到一个混乱的图表,线条来回走动。
看起来日期被忽略了,只考虑了时间戳,这就是图表混乱的原因。我试图编辑日期时间以具有相同的日期和连续的时间戳,并修复了图表。但是,我也必须有日期..
我做错了什么?
【问题讨论】:
-
嗨阿卡,欢迎来到 Stack Overflow!我删除了您的长介绍,如果您愿意,可以将其作为评论重新发布,但这不是问题的必要条件。我们尽量只保留问题中的重要部分,以便以后发现它的其他人可以直接找到好东西!
标签: python datetime graph matplotlib plot