0

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

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 1
    Seaborn is a high-level api for matplotlib. You should plot on plt.subplots with axes level plots. – Trenton McKinney May 04 '23 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 – Trenton McKinney May 04 '23 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](https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html) and pass each instance of your new, e.g., `FacetGridWithFigs` class one of the subfigures. Note that `subfigure`s don't have a `tight_layout` so you need to redefine the `tight_layout` method in your new class (e.g., just to `pass`). – Matt Pitkin May 04 '23 at 15:46
  • I've added my answer here https://stackoverflow.com/a/76175112/1862861 – Matt Pitkin May 04 '23 at 16:25

0 Answers0