How to Facet Plots into a Grid#
This guide shows you how to facet your data into a grid of plots, with one plot per value of one or two categorical variables, using the row and col options. Unlike subplots, the plots of a grid share a single set of axis ticks and labels, which saves space and makes comparisons easier. The data must be long, with a column holding the categories to facet by; if it is wide, reshape it first with melt.
import hvplot.pandas # noqa
import hvplot.xarray # noqa
penguins = hvplot.sampledata.penguins("pandas")
air_temp = hvplot.sampledata.air_temperature("xarray")
Facet into a row of plots#
Set col to a column name to get one plot per value, side by side.
penguins.hvplot.scatter(x='bill_length_mm', y='bill_depth_mm', col='species')
Facet by rows and columns#
Set both row and col to get a 2D grid. The sex column has missing values, which would add a nan facet, so they are dropped first.
penguins.dropna().hvplot.scatter(
x='bill_length_mm', y='bill_depth_mm',
row='sex', col='species',
)
Facet gridded data#
row and col also work with multidimensional arrays, for instance one map per time step.
air_subset = air_temp.isel(time=[0, 1])
air_subset.hvplot(col='time', cmap='viridis', frame_width=200)
Hide the axes#
Turn the axes and colorbar off with xaxis, yaxis and colorbar when they are not needed.
air_subset.hvplot(
col='time', xaxis=False, yaxis=False,
colorbar=False, cmap='viridis', frame_width=200,
)
Combine a grid with by#
Use by to overlay groups inside each plot of the grid. Here each box plot compares the species, and there is one plot per sex.
penguins.dropna().hvplot.box(
y='body_mass_g', by='species',
col='sex', frame_width=200,
)
Next Steps
Split columns or groups into independent subplots
Understand how
by,groupbyandcolordiffer and wide and long dataFacet statistical plots such as histograms and box plots
Use widgets when there are too many facets to show at once
Control plot dimensions with the size and layout options