1

How can I combine two 2 seaborn FacetGrid plots into the same plot side by side?

I can combine plots with ease if the plots are not already sublpots per this question. But how can I get the 2 trellised plots next to each other something like the image below for plots g1 and g2 in the MWE. In the R program I can do this with ggplot2 plots using tools like patchwork.

import seaborn as sns
from matplotlib import pyplot as plt

tips = sns.load_dataset("tips")

g1 = sns.FacetGrid(tips, col="time",  row="sex")
g1.map(sns.scatterplot, "total_bill", "tip")
plt.show()


g2 = sns.FacetGrid(tips, col="size", height=2.5, col_wrap=3)
g2.map(sns.histplot, "total_bill")
plt.show()

enter image description here

4
  • 1
    Seaborn is a high-level api for matplotlib. You should plot on plt.subplots with axes level plots. Commented May 4, 2023 at 15:38
  • Additionally, as per the FacetGrid documentation, use figure level methods like sns.displot directly, instead of FacetGrid with map or map_dataframe Commented May 4, 2023 at 15:45
  • One thing I was able to do (which isn't particularly neat) was subclass the FacetGrid class and allow it to take in a fig argument. You can then use subfigures and pass each instance of your new, e.g., FacetGridWithFigs class one of the subfigures. Note that subfigures don't have a tight_layout so you need to redefine the tight_layout method in your new class (e.g., just to pass). Commented May 4, 2023 at 15:46
  • I've added my answer here stackoverflow.com/a/76175112/1862861 Commented May 4, 2023 at 16:25

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.