Plotly Facets: Small Multiples With facet_col and facet_row

Split a Plotly Express chart into one panel per category with facet_col, facet_row, and facet_col_wrap; clean up the "column=value" panel titles, order the panels, give each panel its own y-axis, add lines to every facet, and fix the spacing error.

Every Plotly Express function takes facet_col= and facet_row=: pass a column name and you get one panel per value, with shared axes so the panels compare directly. facet_col_wrap= wraps a long row of panels onto several rows. Plotly labels each panel column=value, which fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1])) shortens to just the value. The code was written for Plotly 7.2 and also runs on Plotly 6.

Quick Example

import numpy as np
import pandas as pd
import plotly.express as px

# Simulated daily solar and wind output for four grid regions in 2025
rng = np.random.default_rng(41)
days = pd.date_range("2025-01-01", "2025-12-31", freq="D")
summer = -np.cos(2 * np.pi * (days.day_of_year - 15) / 365)  # -1 in mid-January, +1 in mid-July
capacity_gw = {"North": (2, 6), "South": (9, 3), "East": (5, 5), "West": (7, 8)}  # (solar, wind)
frames = []
for region, (solar_gw, wind_gw) in capacity_gw.items():
    for source, gw, cf, sd in [("Solar", solar_gw, 0.17 + 0.10 * summer, 0.05),
                               ("Wind", wind_gw, 0.33 - 0.08 * summer, 0.12)]:
        factor = np.clip(cf + rng.normal(0, sd, len(days)), 0.01, None)
        frames.append(pd.DataFrame({"date": days, "region": region, "source": source,
                                    "gwh": (gw * 24 * factor).round(1)}))
daily = pd.concat(frames, ignore_index=True)
monthly = (daily.groupby([pd.Grouper(key="date", freq="MS"), "region", "source"])["gwh"]
           .sum().reset_index())

fig = px.line(
    monthly, x="date", y="gwh", color="source", facet_col="region", facet_col_wrap=2,
    color_discrete_map={"Solar": "#e9a23b", "Wind": "#2a9d8f"},
    labels={"date": "", "gwh": "GWh per month", "source": ""},
    title="Monthly solar and wind generation by region, 2025", height=520,
)
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.update_xaxes(tickformat="%b")
fig.show()

Plotly line chart split into four panels, one per region (North, South, East, West), showing monthly solar output peaking in summer and wind output peaking in winter

One facet_col argument made four panels that share both axes, so you can see at a glance that the South has the most solar (1,799 GWh at its July peak) and the West the most wind (2,650 GWh in December). In every region, solar peaks in summer and wind in winter. Hover, zoom, and pan work in all panels, and the legend toggles a source everywhere at once.

facet_col, facet_row, and facet_col_wrap

  • facet_col="region" puts one panel per region side by side in a single row.
  • facet_row="source" stacks one panel per source vertically.
  • Use both for a grid: rows for one column, columns for another.
  • facet_col_wrap=2 wraps the facet_col panels after 2 per row. It can't be combined with facet_row: Plotly silently ignores the wrap if facet_row (or marginal=) is set.

Here's a grid of histograms, with the energy source on the rows and the season on the columns:

seasons = {12: "Winter", 1: "Winter", 2: "Winter", 3: "Spring", 4: "Spring", 5: "Spring",
           6: "Summer", 7: "Summer", 8: "Summer", 9: "Fall", 10: "Fall", 11: "Fall"}
daily["season"] = daily["date"].dt.month.map(seasons)

fig = px.histogram(
    daily, x="gwh", facet_row="source", facet_col="season", color="source",
    category_orders={"season": ["Winter", "Spring", "Summer", "Fall"]},
    color_discrete_map={"Solar": "#e9a23b", "Wind": "#2a9d8f"},
    labels={"gwh": "Daily GWh"}, nbins=40, width=900, height=500,
)
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.update_layout(showlegend=False, bargap=0.05)
fig.show()

Plotly histogram grid with two rows (Solar, Wind) and four columns (Winter, Spring, Summer, Fall) of daily generation; solar days cluster near zero in winter and spread out in summer, while wind is highest in winter

The median solar day is 8.6 GWh in winter and 36.4 GWh in summer, while wind runs the other way (49.6 GWh in winter, 31.4 GWh in summer). The source names are on the right side of each row, and the seasons are across the top.

Clean up the facet titles

Plotly writes the panel titles as annotations with the text column=value, such as region=North. There are three common ways to change them:

# 1. Keep only the value: "North"
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))

# 2. Rename the column part with labels=: "Region=North"
# px.line(..., labels={"region": "Region"})

# 3. Map each title to any text you like
names = {"North": "North (mostly wind)", "South": "South (mostly solar)"}
fig.for_each_annotation(lambda a: a.update(text=names.get(a.text, a.text)))

Run option 3 after option 1, since it looks up the bare value. If the category values themselves contain =, use a.text.split("=", 1)[-1] so only the first = is split. The same annotations take styling too, for example a.update(font_size=14). To remove the titles entirely, use fig.for_each_annotation(lambda a: a.update(text="")).

Order the panels with category_orders

Panels appear in the order the values first show up in the data. In the Quick Example that's alphabetical, because groupby sorted the regions. To set the order, pass category_orders={"column": [...]}, as the histogram grid does for the seasons. The same dict also orders the color groups and the categories on a categorical axis:

fig = px.line(monthly, x="date", y="gwh", color="source", facet_col="region", facet_col_wrap=2,
              category_orders={"region": ["West", "South", "East", "North"]})

With facet_col_wrap, the panels fill the grid row by row from the top left.

Give each facet its own y-axis

Facets share their axes by default, which is what you want for comparing sizes. When the panels have very different scales, the small ones flatten into a line along the bottom. Unlink the y-axes with matches=None and turn their tick labels back on:

solar = monthly[monthly["source"] == "Solar"]
fig = px.area(solar, x="date", y="gwh", facet_col="region", facet_col_wrap=2,
              color_discrete_sequence=["#e9a23b"], labels={"date": "", "gwh": "GWh"},
              title="Solar generation, each panel on its own scale", height=520,
              facet_col_spacing=0.07)
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.update_xaxes(tickformat="%b")
fig.update_yaxes(matches=None, showticklabels=True)
fig.show()

Four Plotly area charts of monthly solar generation, one per region, each with its own y-axis range so the North panel (peak about 416 GWh) shows the same summer peak shape as the South (about 1,800 GWh)

North's solar output peaks at 416 GWh, less than a quarter of the South's 1,799 GWh, but on its own axis the shape of its summer peak is clear. facet_col_spacing=0.07 makes room for the extra tick labels between the columns. fig.update_xaxes(matches=None) does the same for the x-axes. Say so in the title or caption, because readers assume facets share a scale.

Add lines and annotations to every facet

add_hline, add_vline, add_hrect, and add_vrect accept row="all", col="all" to draw on every panel. Pass row= and col= numbers to target one panel, but note that facet rows are numbered from the bottom: row=1 is the bottom row, the opposite of make_subplots.

fig = px.line(monthly, x="date", y="gwh", color="source", facet_col="region", facet_col_wrap=2,
              color_discrete_map={"Solar": "#e9a23b", "Wind": "#2a9d8f"},
              labels={"date": "", "gwh": "GWh per month", "source": ""}, height=520)
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.update_xaxes(tickformat="%b")
fig.add_vrect(x0="2025-06-01", x1="2025-08-31", fillcolor="#f4e1b5", opacity=0.6,
              line_width=0, layer="below", row="all", col="all")
fig.add_annotation(text="Summer", x="2025-07-16", y=2500, showarrow=False,
                   row=2, col=1)  # East, the top-left panel, is row 2
fig.show()

Four Plotly line-chart facets of monthly solar and wind generation with a shaded June to August band in every panel and a Summer label in the top-left East panel

The shaded summer band repeats in all four panels from one call, and layer="below" keeps it behind the lines. The "Summer" label goes only on East, the top-left panel, which is row=2, col=1 because the bottom row is row 1. With facet_col_wrap, the panels fill from the top left, but the row numbers still count from the bottom. If you're unsure which panel is which, add the shape to one cell and look, or check which xaxis/yaxis each trace in fig.data uses.

Spacing, size, and too many facets

  • Spacing: facet_col_spacing= (default 0.02) and facet_row_spacing= (default 0.03, or 0.07 with facet_col_wrap) are fractions of the whole figure. Increase them when tick labels collide, for example after showticklabels=True.
  • Size: set height= in the Express call. The panels split the figure, so a 3×3 grid needs roughly twice the height of a single chart.
  • Too many panels: wrapping 60 categories into 2 columns raises
ValueError: Vertical spacing cannot be greater than (1 / (rows - 1)) = 0.034483.
The resulting plot would have 30 rows (rows=30).
Use the facet_row_spacing argument to adjust this spacing.

The default row spacing of 0.07 doesn't fit 30 rows. Pass a smaller facet_row_spacing= (such as 0.01), raise facet_col_wrap, or, better, filter to the categories that matter. Thirty rows of tiny panels are hard to read anyway.

Facets vs make_subplots

You want Use
The same chart split by the values of a column facet_col= / facet_row= in any px function
Different chart types in one figure (a bar next to a pie) make_subplots and add_trace(row=, col=)
Panels of different sizes, or a secondary y-axis make_subplots with specs=
An animation instead of panels animation_frame= (animated bubble charts)

Facets work in px.scatter, px.line, px.area, px.bar, px.histogram, px.box, px.violin, px.density_heatmap, and most other Express functions. They aren't available for charts without x/y axes, such as px.pie, px.sunburst, and px.treemap. The figure is a normal go.Figure, so update_layout, update_traces, and update_xaxes all work on it.

Practical Tips

  • Keep the facet count small enough that each panel is still readable on a phone. Two to six panels is a good range in a blog or report column.
  • Use color= for the comparison that matters most and facets for the rest. Lines in one panel are easier to compare than lines in separate panels.
  • Share axes unless the shapes matter more than the sizes, and label the chart when they don't share.
  • Set category_orders whenever the values have a natural order (months, seasons, sizes).

Similar Topics