How to Split a Plot into Subplots#
This guide shows you how to split a plot into subplots (also called facets), one per column or per group, instead of overlaying everything on a single set of axes.
import hvplot.pandas # noqa
stocks = hvplot.sampledata.stocks("pandas")
penguins = hvplot.sampledata.penguins("pandas")
Split wide data into subplots#
When your data is wide, with one column per series like the stocks below, hvPlot overlays the columns on the same axes by default. Set subplots=True to draw each column in its own plot. Use .cols() to set the number of plots per row, as shown in Lay out plots.
stocks.hvplot(
x='date', y=['Apple', 'Google', 'Meta'], value_label='Stock Price',
subplots=True, width=250, height=200,
).cols(3)
Note
When y is not set, hvPlot plots all the columns except x (and except the columns used by by, groupby, row and col). Setting y explicitly is clearer and avoids subsetting the DataFrame first.
Split long data into subplots#
When your data is long, with a column that labels the groups like species below, combine subplots=True with by to draw one plot per value of that column.
penguins.hvplot.scatter(
x='bill_length_mm', y='bill_depth_mm', by='species',
subplots=True, width=250, height=200,
)
Link or unlink the axes of the subplots#
Subplots that show the same variable have linked axes: zooming or panning one plot updates the others. Set shared_axes=False to give each subplot its own range. Try zooming in the plots below.
stocks.hvplot(
x='date', y=['Apple', 'Google', 'Meta'], value_label='Stock Price',
subplots=True, shared_axes=False, width=250, height=200,
).cols(3)
Next Steps
Arrange plots in a grid of rows and columns with shared axes
Understand wide and long data and how to convert between them
Understand how
by,groupbyandcolordifferExplore groups with widgets instead of showing them all at once