In the code below I can place two simple seaborn plots in one window by passing ax=ax[i]
argument to each, does not work for FacetGrid()
. Similar question has been asked here, wondering if someone has an idea about how to do this. Thanks!
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
df = sns.load_dataset('tips')
########################
# Works :)
########################
fig,ax = plt.subplots(nrows=2)
sns.regplot(x='total_bill', y='tip', data=df, ax=ax[0]) # plot #1
sns.boxplot(x='day', y='total_bill', data=df, ax=ax[1]) # plot #2
plt.show()
########################
# Does not work :(
########################
fig,ax = plt.subplots(nrows=2)
g = sns.FacetGrid(df, col="time", ax=ax[0]) # FacetGrid #1
g.map(plt.hist, "tip")
g = sns.FacetGrid(df, col="sex", hue="smoker", col_wrap=2, ax=ax[1]) # FacetGird #2
g.map(plt.scatter, "total_bill", "tip", alpha=.7)
g.axes[-1].legend()
plt.show()
This is because FacetGrid in itself produces subplots and creates a single figure. Subplots aren't allowed with FacetGrid. The only solution to this is to merge your FacetGrids together to create one plot -- however this is no easy task unless your axis are the same.
Workarounds are possible as described here: combine different seaborn facet grids into single plot