Plot datetime.time in seaborn

最后都变了- 提交于 2021-01-22 04:25:27

问题


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'

This is my 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

This is my code:

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

UPDATE:

dtype of 'toronto_time' is an object. When I used pd.to_datetime it converts to datetime however it adds a date.


回答1:


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



来源:https://stackoverflow.com/questions/52289579/plot-datetime-time-in-seaborn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!