Small-multiple relationship plots
Compare a relationship across categories
Seaborn · Class
FacetGrid builds a grid of Matplotlib axes for conditional plots, organized by row and column variable levels. An optional hue variable adds colored subsets, after which plotting and formatting methods can be applied.
FacetGrid creates a Matplotlib figure containing multiple axes arranged by levels of row and column variables, with an optional hue dimension for colored subsets. After construction, map a plotting function across each subset and then adjust labels, ticks, legends, or layout with the grid’s methods.
seaborn.FacetGrid(data, *, row=None, col=None, hue=None, col_wrap=None, sharex=True, sharey=True, height=3, aspect=1, palette=None, row_order=None, col_order=None, hue_order=None, hue_kws=None, dropna=False, legend_out=True, despine=True, margin_titles=False, xlim=None, ylim=None, subplot_kws=None, gridspec_kws=None)| Name | Type | Description |
|---|---|---|
data | DataFrame | Input dataset containing the variables used to form facets. |
row | vector or key in data | Variable defining row facets. |
col | vector or key in data | Variable defining column facets. |
hue | vector or key in data | Variable defining colored subsets. |
col_wrap | int | Number of columns before wrapping to another row. |
sharex, sharey | bool | Whether facet axes share x and y scales. |
height, aspect | float | Figure height per facet and width-to-height ratio. |
palette, row_order, col_order, hue_order | palette or sequence | Color palette and explicit orders for facet levels. |
hue_kws | dict | Keyword mappings for hue levels. |
dropna | bool | Whether to remove missing observations. |
legend_out, despine, margin_titles | bool | Legend placement, despining, and margin-title behavior. |
xlim, ylim, subplot_kws, gridspec_kws | tuple or dict | Initial axis limits and subplot/grid layout options. |
A FacetGrid object that initializes the Matplotlib figure and axes arrangement.
import seaborn as sns
import matplotlib.pyplot as plt
penguins = sns.load_dataset("penguins")
grid = sns.FacetGrid(penguins, col="species", hue="sex", height=3, aspect=1)
grid.map(sns.scatterplot, "bill_length_mm", "bill_depth_mm")
grid.add_legend()
plt.show()Creates one scatterplot per species and uses color to distinguish sex.
Compare a relationship across categories
Show distributions separately for combinations of discrete groups
Add a third categorical grouping through color
The constructor receives a dataset and optional row, column, and hue variables. It creates the figure and axes grid but does not itself draw a data plot; plotting is added afterward with methods such as map or map_dataframe.
Follow-up: How would you control the ordering of facet levels? Supply row_order, col_order, or hue_order with the desired category sequences.
row and col determine the two-dimensional axes arrangement. hue adds a third grouping dimension by drawing different subsets in different colors within each facet.
Follow-up: What if the hue grouping needs per-level style changes? Use hue_kws to provide keyword values associated with hue levels.
map applies a plotting callable to every facet subset, passing the selected data columns in the order named. The callable must draw on the active Matplotlib Axes and accept color; with hue faceting it must also accept label.
Follow-up: When is map_dataframe preferable? Use it when the plotting function should receive each subset as a DataFrame and access additional named columns or variables.
FacetGrid.mapFacetGrid.map_dataframeFacetGrid.facet_dataFacetGrid.applyFacetGrid.add_legendFacetGrid.setset_theme