1
column1       column2    column3    column4   
a            1990        25         A 
b            1990        27         A 
c            1990        24         A
a            1991        26         B
b            1990        20         B
c            1990        20         C

so subset where column1 == a should be like

column1       column2    column3    column4   
a            1990        25         A 
a            1991        26         B 
a            1990        20         C

I wrote a code to generage multiple plots of subset of pandas data frame subset is the set of rows where the column1 value is same in element of list in sub

sub = ['a','b','c','d','e']

for i in sub:
    df_s = df[df['column1']==i]
    plt.figure(figsize=(5,5))
    sns.lineplot(x='column2',y='column3', data=df_s, hue='column4',legend=False)
    plt.title("Data of " + p)
plt.show

the plots are generated separately with my code. but I want to make it in one figure and only one legend.

4
  • if you move plt.figure(figsize=(5,5)) before the loop for, then all the lines will be on the same graph, but it will look a bit messy I think. Could you add some input data to see what it looks like Commented Jun 14, 2020 at 2:20
  • 1
    @Ben.T I edited the question with some example input data Commented Jun 14, 2020 at 16:49
  • ok now a question, because you use hue='column4', now you have let's say A blue, B red and C green, this on each plot for a, b, c, d, e. If you want all of them on one plot, how do you see it, somthing like a-A blue, a-B red, a-C green, b-A yellow, b-B orange ... or you want something different? like using markers for each sub, or...? Commented Jun 14, 2020 at 17:35
  • No want to see the plot for a, b, c, seperately but in one figure. so the color style? like legend is same at all plots. but each plot only represent the subset of data frame df_s. So what I want to see is 5 plots where the data is df_s, in one figure. My data has 26 elements in sub list so, 26 plots. But when I run that code, all the plots are just in one line and I can't save the all plots in one figure Commented Jun 14, 2020 at 20:44

1 Answer 1

1

So I think you need to use FaceGrid, here is one way. I used fake data I created

sub = ['a','b','c','d','e']
#select just the sub data at once
dfs = df[df['column1'].isin(sub)]

#create the FaceGrid
g = sns.FacetGrid(dfs, 
                  col="column1", 
                  hue='column4', 
                  col_wrap=2, # here it means 2 columns depending on the position you want
                  legend_out=True) 

#lineplot each sub
g.map(sns.lineplot, 'column2', 'column3').add_legend()

#some parameters for titles, axis and legend name if you want to change them
g.set_titles("Data of {col_name}")
g.set_axis_labels(x_var="Maybe something", 
                  y_var="Something else")
g._legend.set_title('name')

#if you want to save the figure
g.savefig('test.png')

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.