Python / Data Science Essentials Interview Questions
What is Seaborn's FacetGrid and how does it enable multi-panel statistical plots?
FacetGrid is Seaborn's mechanism for trellis/small-multiples plots — the same chart repeated across different subsets of the data, defined by one or more categorical columns. It is one of Seaborn's most powerful features for exploring interaction effects between variables.
import seaborn as sns import matplotlib.pyplot as plt tips = sns.load_dataset('tips') # --- FacetGrid manually --- g = sns.FacetGrid(tips, col='time', row='sex', height=3, aspect=1.2) g.map_dataframe(sns.histplot, x='total_bill', bins=15, kde=True) g.add_legend() g.set_titles(col_template='{col_name} service', row_template='Sex: {row_name}') g.set_axis_labels('Total Bill ($)', 'Count') # --- Figure-level functions (wrap FacetGrid automatically) --- # relplot â relational sns.relplot(data=tips, x='total_bill', y='tip', col='smoker', hue='sex', kind='scatter', height=4) # displot â distributional sns.displot(data=tips, x='total_bill', col='sex', row='time', kind='kde', fill=True) # catplot â categorical sns.catplot(data=tips, x='day', y='tip', col='sex', kind='violin', height=5, aspect=0.8)
The figure-level functions (relplot, displot, catplot) return a FacetGrid object, not an Axes. To customise them after creation you call FacetGrid methods like g.set_titles(), g.set_axis_labels(), or iterate over g.axes.flatten() to access individual Axes objects and apply standard Matplotlib customisation.
More Related questions...