Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Seaborn relplots single rows into one figure

I created three figures using seaborn relplot. Each one is a single row with a different y variable, but the same x, column, and hue variables. I want to visualize these three rows all on the same figure. I could make a new figure and copy the contents over, but that would lose the formatting and I am hoping seaborn offers this functionality, but I cannot find it.

My data is in a pandas dataframe named sample:

In: sample                                                           
Out: 
     X Val  Col Val  Hue Val  Y Val Row 1  Y Val Row 2  Y Val Row 3
0     0.95      0.4      0.4     0.638958     0.635887         1061
1     0.96      0.4      0.4     0.639547     0.654841          976
2     0.97      0.4      0.4     0.638076     0.675838          916
3     0.98      0.4      0.4     0.633368     0.689067          831
4     0.99      0.4      0.4     0.629690     0.711538          719
..     ...      ...      ...          ...          ...          ...
120   0.95      0.8      0.8     0.634979     0.624095         1066
121   0.96      0.8      0.8     0.635565     0.642270          986
122   0.97      0.8      0.8     0.635272     0.665792          916
123   0.98      0.8      0.8     0.633221     0.687564          826
124   0.99      0.8      0.8     0.629852     0.712159          706

[125 rows x 6 columns]

I have created three relplot figures:

import seaborn as sns
g1 = sns.relplot(data=sample, x="X Val", y = "Y Val Row 1", col="Col Val", hue="Hue Val", kind="line")
g3 = sns.relplot(data=sample, x="X Val", y = "Y Val Row 3", col="Col Val", hue="Hue Val", kind="line")
g2 = sns.relplot(data=sample, x="X Val", y = "Y Val Row 2", col="Col Val", hue="Hue Val", kind="line")

Each figure (g1, g2, g3) is a single row of five axes objects, each with the same column values, hue values, and x values. I want to combine these into one figure of these three rows stacked. I combined the figures in power point to show what I want for the final result:

Complete figure, with g1 as row 1, g2 as row 2, and g3 as row 3:

Complete figure, with g1 as row 1, g2 as row 2, and g3 as row 3

like image 652
heidi Avatar asked Aug 31 '25 03:08

heidi


1 Answers

Thanks to @JohanC's comments, I now have the solution.

# melt the dataframe so all the "Y Val Row *" values are in the same column
samplemelt = pd.melt(sample, id_vars=['X Val', 'Col Val', 'Hue Val'], 
                     value_vars=['Y Val Row 1', 'Y Val Row 2', 'Y Val Row 3'],
                     var_name="variable", value_name="value")

# make the relplot, limiting the sharey flag to each row
gm = sns.relplot(data=samplemelt, row="variable", y="value", x="X Val", 
                 col="Col Val", hue="Hue Val", facet_kws={"sharey":"row"})

# pull the "Y Val Row *" label from each graph title onto the row y label
# this assumes the current default behavior seaborn.relplot for graph titling
for row in gm.axes:
    for ax in row:
        title = ax.title.get_text()
        ax.set_title(title.split(" | ")[1])
    row[0].set_ylabel(title.split(" | ")[0].replace("variable = ", ""))
like image 139
heidi Avatar answered Sep 02 '25 19:09

heidi