Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot datetime.time in seaborn

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.

like image 756
Nikko Avatar asked Sep 12 '18 07:09

Nikko


People also ask

How do you plot time data in Python?

In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.

Does Seaborn have interactive plots?

Behind the scenes, seaborn uses matplotlib to draw its plots. For interactive work, it's recommended to use a Jupyter/IPython interface in matplotlib mode, or else you'll have to call matplotlib. pyplot. show() when you want to see the plot.


1 Answers

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

enter image description here

like image 60
Joe Avatar answered Oct 08 '22 20:10

Joe