跨列迭代时绘制线图Seaborn

时间:2019-07-03 17:24:06

标签: python pandas dataframe

给出:

    Month = ["Jan","Feb","Mar","Apr","May","Jun"]
Apple= [500,180,1141, 1209, 600,1200]
Orange= [900,350,198,789,650,500]
Cherry = [852,415,874,404, 692,444]

list = {'Month': Month,
       'Apple': Apple,
       'Orange': Orange,
       'Cherry': Cherry}

我正在尝试绘制一个折线图,其中x = Month和y = Apple:Cherry在1)一张图以及所有3个变量(Apple,Orange和Cherry)和2)折线图中每个变量(x =月,y =苹果等)。

我已经尝试过遍历各列,如下所示,但似乎无法通过Seaborn进行工作:

for i in range (df.shape[1]-1):
    sns.lineplot(x=df[:,0], y=df[:,i+1])

1 个答案:

答案 0 :(得分:1)

IIUC,您想要hue中的seaborn

df = pd.DataFrame(lst)
new_df = df.melt(id_vars='Month', 
                 value_name='val', 
                 var_name='type')

sns.lineplot(x='Month', y='val', hue='type', data=new_df)

输出:

enter image description here