您好,我在使用 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()
关于python - 在 seaborn 中绘制 datetime.time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52289579/