3

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

heidi
  • 96
  • 5
  • 2
    You probably need to convert the 3 "Y Val Row" columns to "long form" via `sample.melt(...)`. Then, you can use the newly created identifier column for `sns.relplot(...., rows=...)`. – JohanC Feb 18 '21 at 16:42
  • @JohanC's answer worked: Melt the dataframe: `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'])` , then make the relplot: `gm = sns.relplot(data=samplemelt, row="variable", y="value", x="X Val", col="Col Val", hue="Hue Val", kind="line")` But now I am trying to remove the sharey behavior, because the different variables that have all been melted together are on different scales. This is proving to be more difficult than expected! – heidi Feb 18 '21 at 19:02
  • 2
    `sns.relplot(...., facet_kws={'sharey': 'row'})` ? [Prevent Sharing of Y Axes in Seaborn Relplot?](https://stackoverflow.com/questions/56363662/prevent-sharing-of-y-axes-in-seaborn-relplot) – JohanC Feb 18 '21 at 19:52
  • Oops...I didn't dig far enough down to find that. Thank you so much!! – heidi Feb 19 '21 at 14:19

1 Answers1

1

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 = ", ""))
heidi
  • 96
  • 5