0

Given:

    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}

I'm trying to plot a line graph where x= Month and y= Apple:Cherry in 1) one graph together with all 3 variables (Apple, Orange and Cherry) and 2) line graph with each variable (x= Month, y= Apple, etc).

I've tried iterating across columns as seen below, but it doesn't seem to work via Seaborn:

for i in range (df.shape[1]-1):
    sns.lineplot(x=df[:,0], y=df[:,i+1])
D500
  • 442
  • 5
  • 17
  • 2
    First do not name any self define object by list or dict – BENY Jul 03 '19 at 17:30
  • 2
    Note that seaborn *will be most powerful when your datasets have a particular organization. This format is alternately called “long-form” or “tidy” data*, as explained [here](https://seaborn.pydata.org/introduction.html#organizing-datasets) (and likely in other places) – Brendan Jul 03 '19 at 17:34

1 Answers1

1

IIUC, you want hue in 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)

Output:

enter image description here

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • How do I create separate plots? I tried the iterate statement because I wanted seperate subplots for each variable. – D500 Jul 03 '19 at 17:43
  • Do you really need seaborn: `df.plot(x='Month',subplots=True)`? – Quang Hoang Jul 03 '19 at 17:44
  • With Seaborn, plotting x= month takes it out of order. For instance, it starts with Apr, Feb, Jan.. Order isn't a function either. Suggestions? – D500 Jul 03 '19 at 18:35
  • plot against the index and change the tick labels by `ax.set_xticklabels()`? – Quang Hoang Jul 03 '19 at 18:39