Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 42 ยท Subplots and Annotations

Facets, Small Multiples and Insets

Split one dataset into many panels with Plotly Express facet_row and facet_col, control wrapping and axes, then add inset charts and multiple y-axes with domains.

Suppose you have life expectancy for five continents. One scatter plot with five colors works, but when the points overlap you lose the picture. A better design is small multiples: the same chart repeated once per group, side by side, with identical axes. Your eye compares shapes instantly. Plotly Express builds this for you with facets, and you can go further with insets and extra axes using the same domain idea from make_subplots.

Facets in Plotly Express

Almost every px function accepts these arguments:

  • facet_col="column": one panel per distinct value, arranged in a row.
  • facet_row="column": one panel per value, stacked vertically.
  • Both together: a grid of every combination.
  • facet_col_wrap=3: wrap the columns after 3 panels so a long list does not become a thin strip.
  • facet_col_spacing, facet_row_spacing: gaps between panels.
  • category_orders={"day": [...]}: control the panel order.
Python One panel per continent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each panel title is again an annotation, such as continent=Asia. Facets are plain subplots: Plotly Express created xaxis ... xaxis6 and the matching yaxises with domains, exactly as make_subplots would. Facet axes are matched by default, so all panels share one range and the comparison is honest.

Rows, columns and the grid

Python A day-by-time grid of tips

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Empty combinations (nobody paid for Saturday lunch in this data) simply show an empty panel. Always keep the number of panels modest: a facet grid of six to twelve panels is about the limit before the labels shrink to nothing.

Controlling the facet axes

Because the panels match their axes, a panel with small values can look flat next to a large one. To give every panel its own scale:

Python
fig.update_yaxes(matches=None, showticklabels=True)
fig.update_xaxes(matches=None)

matches=None removes the link. Free scales help you see shape, but they remove comparability, so say so in the title. You can also relabel the panel titles by editing the annotations:

Python
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

for_each_annotation, for_each_xaxis, for_each_yaxis and for_each_trace visit every element and are the standard way to restyle a faceted figure. fig.update_traces(marker_size=6) and fig.update_layout(...) still work as usual.

Use facet_col_wrap (with facet_col only) for many panels, and facet_row plus facet_col for exactly two categorical variables. Never combine facet_row with a long list of values: the figure becomes a tall, hard-to-read column.

Insets: a small chart inside a big one

An inset is a second pair of axes placed over the main plot. With the Plotly.js layout you give the second axes a small domain; with Plotly Python you do the same through layout.xaxis2 and layout.yaxis2. No overlaying is needed: place the domain in a corner and anchor the new axes to each other.

A zoomed inset inside the main plot

The dotted rectangle on the main chart marks what the inset shows, using the shapes you already know. In Python the same figure is built by assigning fig.update_layout(xaxis2=dict(domain=[0.58, 0.95], anchor="y2"), yaxis2=dict(domain=[0.55, 0.95], anchor="x2")) and adding a trace with xaxis="x2", yaxis="y2".

Several y-axes on one chart

When two series share x but not units, you can stack axes side by side: give each trace its own yaxis ("y", "y2", "y3") and place them with overlaying="y" and side. A cleaner solution for exactly two series is secondary_y from make_subplots. For three or more, consider separate stacked subplots with a shared x-axis, which is far easier to read than three scales on one plot.

Choosing the right layout tool

flowchart TD
    A["Many charts needed"] --> B{"Same chart, many groups?"}
    B -->|Yes| C["px facet_col / facet_row"]
    B -->|No| D{"Different chart types or sizes?"}
    D -->|Yes| E["make_subplots with specs"]
    D -->|No| F{"Small detail inside a big one?"}
    F -->|Yes| G["Inset: extra axes with a small domain"]
    F -->|No| H["Separate figures or tabs"]

Facets for repeated charts, make_subplots for mixed layouts, an inset for a zoom.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which px argument wraps panels into several lines after three columns?

    facet_col_wrap sets how many facet columns appear before wrapping to the next line.
  2. 2. How do you give every facet panel its own y-axis scale?

    Facet axes are linked with matches. Setting matches to None unlinks them.
  3. 3. What makes an inset chart in Plotly?

    An inset is just another subplot whose domain covers a small corner.
  4. 4. Why are facet axes matched by default?

    Small multiples work because every panel uses the same scale, so differences in shape are real differences in the data.

Summary

  • facet_col, facet_row and facet_col_wrap turn one px call into small multiples.
  • Panels are subplots with matched axes. Unlink them with matches=None when needed.
  • for_each_annotation and for_each_trace restyle every panel at once.
  • An inset is an extra axis pair with a small domain; mark the zoomed region with a shape.
  • For two units use secondary_y; for more, stack subplots.

Next: 3D Scatter and Line Plots