我在一列中有日期,在另一列中有时间,我通过pandas read_sql从数据库中检索到。dataframe如下所示(在我的daaframe中有30-40行)。我想把它们画成时间序列图。如果我想,我应该在一个位置转换成直方图以及
COB CALV14
1 2019-10-04 07:04
2 2019-10-04 05:03
3 2019-10-03 16:03
4 2019-10-03 05:15
首先,我得到了不同的错误-比如没有数字字段来绘图等。在搜索了很多之后,我能找到的最接近的帖子是:Matplotlib date on y axis
我跟进并获得了一些结果——但问题是:
I have to follow number of steps (convert to str then list and then to matplot lib datetime format) before I can plot them. (Please refer the code I am using) There must be a smarter and more precise way to do this. This does not show the time beside the axis the way they exactly appear in the data frame. (eg it should show 07:03, 05:04 etc)
python新手-非常感谢您在这方面的帮助
代码
ob_frame['COB'] = ob_frame.COB.astype(str)
ob_frame['CALV14'] = ob_frame.CALV14.astype(str)
date = ob_frame.COB.tolist()
time = ob_frame.CALV14.tolist()
y = mdates.datestr2num(date)
x = mdates.datestr2num(time)
fig, ax = plt.subplots(figsize=(9,9))
ax.plot(x, y)
ax.yaxis_date()
ax.xaxis_date()
fig.autofmt_xdate()
plt.show()
我找到了答案。我不需要将从DB检索到的数据转换为字符串类型。其余的问题我被认为是因为没有为记号标签使用正确的格式。这里有完整的代码张贴,以防这将有助于任何人。 在这段代码中,我改变了Y轴和X轴:我在X轴上绘制了日期,在Y轴上绘制了时间,因为它看起来更好
输出:
相关问题 更多 >
编程相关推荐