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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With