Multi-line chart with seaborn tsplot

浪子不回头ぞ 提交于 2019-12-17 20:54:06

问题


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

回答1:


The tsplot is a bit strange or at least strangly documented. If a dataframe is supplied to it, it assumes that there must be a unit and a time column present, as it internally pivots about those two. To use tsplot to plot several time series you would therefore need to supply an argument to unit as well; this can be the same as condition.

sns.tsplot(df, time='hour', unit = "direction", 
               condition='direction', value='hourly_avg_count')

Complete example:

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as 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 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.




回答2:


Try adding a dummy unit column. The first parts is to create some synthetic data, so please ignore.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

df1 = pd.DataFrame({
"hour":range(24),
"direction":1,
"hourly_avg_count": np.random.randint(25,28,size=24)})

df2 = pd.DataFrame({
"hour":range(24),
"direction":2,
"hourly_avg_count": np.random.randint(25,28,size=24)})

df = pd.concat([df1,df2],axis=0)
df['unit'] = 'subject'

plt.figure()
sns.tsplot(data=df, time='hour', condition='direction',
unit='unit', value='hourly_avg_count')



来源:https://stackoverflow.com/questions/45327767/multi-line-chart-with-seaborn-tsplot

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