[英]seaborn multiple lines in a Lineplot
I have a dataframe looks like the following:我有一个如下所示的数据框:
location![]() |
1/1/21 ![]() |
1/2/21 ![]() |
1/3/21 ![]() |
another_var![]() |
---|---|---|---|---|
A![]() |
13 ![]() |
15 ![]() |
18 ![]() |
0.25 ![]() |
B![]() |
1 ![]() |
4 ![]() |
4 ![]() |
0.28 ![]() |
C ![]() |
3 ![]() |
3 ![]() |
3 ![]() |
0.3 ![]() |
location column is the index column How can I make a seaborn lineplot with three lines (A, B, C) and dates on the x-axis? location 列是索引列 如何制作带有三行(A、B、C)和 x 轴上的日期的 seaborn 线图? I tried to do df.columns but I don't know how to exclude the last column (since it's not a date column)
我试图做 df.columns 但我不知道如何排除最后一列(因为它不是日期列)
You melt a subset of the dataframe to give you something in the long format:您融合了数据帧的一个子集,以提供长格式的内容:
import pandas as pd
import seaborn as sns
df = pd.DataFrame({'1/1/21':[13,1,3],"1/2/21":[15,4,3],"1/3/21":[18,4,3],
"another_var":[0.25,0.28,0.3]},index=["A","B","C"])
df.iloc[:,:-1].reset_index().melt(id_vars="index")
index variable value
0 A 1/1/21 13
1 B 1/1/21 1
2 C 1/1/21 3
3 A 1/2/21 15
4 B 1/2/21 4
5 C 1/2/21 3
6 A 1/3/21 18
7 B 1/3/21 4
8 C 1/3/21 3
And you plot this:你绘制这个:
sns.lineplot(data = df.iloc[:,:-1].reset_index().melt(id_vars="index"),
x = "variable" , y = "value" , hue = "index")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.