python - Multi-line chart with seaborn tsplot -
i want create smoothed line chart using matplotlib , seaborn.
this 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 2 lines, 1 direction
equal 1, direction
equal 2. x axis hour
, y axis hourly_avg_count
.
i tried this, cannot see lines.
import pandas pd import seaborn sns import matplotlib import matplotlib.pyplot plt plt.figure(figsize=(12,8)) sns.tsplot(df, time='hour', condition='direction', value='hourly_avg_count')
the tsplot
bit strange or @ least strangly documented. if dataframe supplied it, assumes there must unit
, time
column present, internally pivots two. use tsplot
plot several time series therefore need supply argument unit
well; can same condition
.
sns.tsplot(df, time='hour', unit = "direction", condition='direction', value='hourly_avg_count')
complete example:
import numpy np import pandas pd import seaborn sns import matplotlib.pyplot plt hour, direction = np.meshgrid(np.arange(24), np.arange(1,3)) df = pd.dataframe({"hour": hour.flatten(), "direction": direction.flatten()}) df["hourly_avg_count"] = np.random.randint(14,30, size=len(df)) plt.figure(figsize=(12,8)) sns.tsplot(df, time='hour', unit = "direction", condition='direction', value='hourly_avg_count') plt.show()
also worth noting tsplot deprecated of seaborn version 0.8. might worth use other way plot data anyways.
Comments
Post a Comment