带有 seaborn tsplot 的多折线图
Multi-line chart with seaborn tsplot
我想使用 matplotlib 和 seaborn 创建一个平滑的折线图。
这是我的数据框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
... ... ...
折线图应包含两条线,一条表示direction
等于1,另一条表示direction
等于2。X轴为hour
,Y轴为hourly_avg_count
.
我试过了,但看不到线条。
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')
尝试添加虚拟单位列。第一部分是创建一些合成数据,所以请忽略。
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')
tsplot
有点奇怪,或者至少有奇怪的记录。如果向它提供数据框,它假定必须存在一个 unit
和一个 time
列,因为它在内部以这两个为中心。因此,要使用 tsplot
绘制多个时间序列,您还需要为 unit
提供一个参数;这可以与 condition
.
相同
sns.tsplot(df, time='hour', unit = "direction",
condition='direction', value='hourly_avg_count')
完整示例:
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()
还值得注意的是 tsplot is deprecated 从 seaborn 版本 0.8 开始。因此,无论如何使用其他方法绘制数据可能是值得的。
我想使用 matplotlib 和 seaborn 创建一个平滑的折线图。
这是我的数据框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
... ... ...
折线图应包含两条线,一条表示direction
等于1,另一条表示direction
等于2。X轴为hour
,Y轴为hourly_avg_count
.
我试过了,但看不到线条。
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')
尝试添加虚拟单位列。第一部分是创建一些合成数据,所以请忽略。
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')
tsplot
有点奇怪,或者至少有奇怪的记录。如果向它提供数据框,它假定必须存在一个 unit
和一个 time
列,因为它在内部以这两个为中心。因此,要使用 tsplot
绘制多个时间序列,您还需要为 unit
提供一个参数;这可以与 condition
.
sns.tsplot(df, time='hour', unit = "direction",
condition='direction', value='hourly_avg_count')
完整示例:
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()
还值得注意的是 tsplot is deprecated 从 seaborn 版本 0.8 开始。因此,无论如何使用其他方法绘制数据可能是值得的。