在seaborn中绘制datetime.time [英] Plot datetime.time in seaborn
本文介绍了在seaborn中绘制datetime.time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在与seaborn约会时遇到了麻烦.我正在尝试使用x作为数据类型 datetime.time
绘制分类数据,但出现以下错误:
Hi I am having trouble plotting a datetime with seaborn. I am trying to plot a categorical data with x as datatype datetime.time
but I get these error:
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 但它会添加一个日期.
dtype of 'toronto_time' is an object. When I used pd.to_datetime it converts to datetime however it adds a date.
推荐答案
如果我理解正确,你可以这样做:
If I understood you correctly, you could do in this way:
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()
这篇关于在seaborn中绘制datetime.time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文