I want to create a smoothed line chart using matplotlib and seaborn.
This is my dataframe df
:
hour direction hourly_avg_count
0 1 20
1 1 22
2 1 21
3 1 21
.. ... ...
24 1 15
0 2 24
1 2 28
... ... ...
The line chart should contain two lines, one for direction
equal to 1, another for direction
equal to 2. The X axis is hour
and Y axis is hourly_avg_count
.
I tried this, but I cannot see the lines.
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
plt.figure(figsize=(12,8))
sns.tsplot(df, time='hour', condition='direction', value='hourly_avg_count')
The
tsplot
is a bit strange or at least strangly documented. If a dataframe is supplied to it, it assumes that there must be aunit
and atime
column present, as it internally pivots about those two. To usetsplot
to plot several time series you would therefore need to supply an argument tounit
as well; this can be the same ascondition
.Complete example:
Also worth noting that tsplot is deprecated as of seaborn version 0.8. It might thus be worth to use some other way to plot the data anyways.
Try adding a dummy unit column. The first parts is to create some synthetic data, so please ignore.