gpt4 book ai didi

python - 在 seaborn 中绘制 datetime.time

转载 作者:行者123 更新时间:2023-11-28 17:04:10 25 4
gpt4 key购买 nike

您好,我在使用 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 时,它​​会转换为日期时间,但会添加一个日期。

最佳答案

如果我没理解错的话,你可以这样做:

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()

enter image description here

关于python - 在 seaborn 中绘制 datetime.time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com