python - 在 seaborn 中绘制 datetime.time

标签 python pandas matplotlib seaborn

您好,我在使用 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/

相关文章:

python - 在 Windows 上安装 firebird RDBMS 绑定(bind)包时出错

python - for 循环应该循环 9 次,而不是循环一次

python - 如何在 Python Pandas Dataframe 中有条件地连接 2 列

python , Pandas : Assign cells in dataframe to groups

python - pandas Group通过绘制每个组

python - 具有均匀间隔颜色的颜色条的不均匀(不规则)间隔数据

python - matplotlib 中的直方图在顶部被裁剪

Python PANDAS : New Column, 将唯一值应用于所有行

python - python中的金融技术分析

python - 获取用户输入以选择 Pandas 对象