【问题标题】:Plot datetime.time in seaborn在 seaborn 中绘制 datetime.time
【发布时间】:2019-02-16 18:21:01
【问题描述】:

您好,我无法使用 seaborn 绘制日期时间。我正在尝试使用 x 作为数据类型 datetime.time 绘制分类数据,但出现以下错误:

float() argument must be a string or a number, not 'datetime.time'

这是我的 df:

       toronto_time             description
0      00:00:50                   STATS
1      00:01:55                   STATS
2      00:02:18                   ONLINE
3      00:05:24                   STATS
4      00:05:34                   STATS
5      00:06:33                   OFFLINE

这是我的代码:

import matplotlib.pyplot as plt
import seaborn as sns

plt.style.use('seaborn-colorblind')

plt.figure(figsize=(8,6))
sns.swarmplot('toronto_time', 'description', data=df);
plt.show()

更新:

'toronto_time' 的 dtype 是一个对象。当我使用 pd.to_datetime 时,它​​会转换为 datetime,但是会添加一个日期。

【问题讨论】:

标签: python pandas matplotlib seaborn


【解决方案1】:

如果我理解正确,你可以这样做:

import matplotlib.pyplot as plt
import seaborn as sns
df['toronto_time'] = pd.to_datetime(df['toronto_time']).dt.strftime('%H:%M:%S')
sns.scatterplot(df['toronto_time'], df['description'])
plt.show()

【讨论】:

  • 哇!谢谢!现在我正在考虑如何将 xticks 设置为仅“小时”(0:00 - 24:00)
猜你喜欢
  • 1970-01-01
  • 2015-10-06
  • 2015-12-28
  • 2018-05-13
  • 1970-01-01
  • 2012-01-12
  • 2017-05-10
  • 2019-08-22
  • 1970-01-01
相关资源
最近更新 更多