Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Seaborn · Class

FacetGrid

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
dataDataFrameInput dataset containing the variables used to form facets.
rowvector or key in dataVariable defining row facets.
colvector or key in dataVariable defining column facets.
huevector or key in dataVariable defining colored subsets.
col_wrapintNumber of columns before wrapping to another row.
sharex, shareyboolWhether facet axes share x and y scales.
height, aspectfloatFigure height per facet and width-to-height ratio.
palette, row_order, col_order, hue_orderpalette or sequenceColor palette and explicit orders for facet levels.
hue_kwsdictKeyword mappings for hue levels.
dropnaboolWhether to remove missing observations.
legend_out, despine, margin_titlesboolLegend placement, despining, and margin-title behavior.
xlim, ylim, subplot_kws, gridspec_kwstuple or dictInitial axis limits and subplot/grid layout options.

Returns

A FacetGrid object that initializes the Matplotlib figure and axes arrangement.

Key points

  • Use FacetGrid to display conditional relationships as a coordinated set of plots.
  • Rows and columns correspond to levels of variables, producing one axes position per facet combination.
  • The hue dimension separates additional subsets by color inside the row-and-column grid.
  • Build the grid first, then apply one or more plotting functions to every subset.
  • Use subsequent grid methods to refine axes, ticks, labels, legends, and related presentation details.

Common mistakes

  • Pass long-form data to the constructor rather than a wide table whose columns do not represent observation variables.
  • Each facet is a separate small multiple, so many category levels can create a crowded or very large figure.

Example: Faceted scatterplots

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.

When to use it

Small-multiple relationship plots

Compare a relationship across categories

Conditional distributions

Show distributions separately for combinations of discrete groups

Three-dimensional categorical faceting

Add a third categorical grouping through color

Interview questions

QWhat does FacetGrid construct, and what is the usual workflow?

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.

QHow do row, col, and hue differ?

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.

QWhat requirements does a function passed to map have?

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.map
  • FacetGrid.map_dataframe
  • FacetGrid.facet_data
  • FacetGrid.apply
  • FacetGrid.add_legend
  • FacetGrid.set
  • set_theme

Source: seaborn.FacetGrid — seaborn 0.13.2 documentation. Checked 2026-10-15.