【问题标题】:i get keyerror on 'time' when trying to scatter plot, how to scatter plot time?尝试散点图时,我在“时间”上遇到键错误,如何散点图时间?
【发布时间】:2018-03-05 06:47:47
【问题描述】:

当我尝试散点图时,“时间”出现键错误,我如何散点图这些列?

 high      time
0    17.23  10:37:00
1    17.64  14:01:00
2    15.28  09:31:00
3    15.56  11:41:00
4    15.56  11:42:00
5    15.25  09:30:00
6    15.25  09:49:00
7    15.10  15:52:00
8    16.41  10:40:00
9    17.75  15:09:00
10   17.75  15:10:00
11   19.20  09:41:00

df_high.plot.scatter(x='time', y='high', c=None, s=None)
plt.show()

【问题讨论】:

  • 我怀疑 dtype 没有被自动设置。请与df['time'].dtype 联系。您可能需要 datetime dtype。可以使用 pd.to_datetime 之类的东西进行转换,或者更好地使用适当的 dtype 加载数据。

标签: python pandas matplotlib scatter-plot


【解决方案1】:
df.set_index('time').sort_index().plot(style='.')

【讨论】:

  • 谢谢你……那行得通。只是让我理解得更好一些,我的“时间”列可能不是日期时间格式?或者,我应该将我的“时间”列作为索引吗?
  • @RossDemtschyna 是的,至少使用索引是最安全的方式。
猜你喜欢
  • 1970-01-01
  • 2021-06-02
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 2011-11-01
  • 2016-03-15
  • 1970-01-01
相关资源
最近更新 更多