Plotly Subplots and Facets
Two ways to create multi-plot layouts
- Facets (Plotly Express): easiest for grid layouts
- Subplots (plotly.subplots): full control
Facets with Plotly Express
Facet example
import plotly.express as px
df = px.data.tips()
fig = px.scatter(
df,
x="total_bill",
y="tip",
color="sex",
facet_col="day",
title="Tips by day (facets)",
)
fig.show()Facet example
import plotly.express as px
df = px.data.tips()
fig = px.scatter(
df,
x="total_bill",
y="tip",
color="sex",
facet_col="day",
title="Tips by day (facets)",
)
fig.show()Manual subplots
Subplots
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=["A", "B"])
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9], mode="lines", name="line"), row=1, col=1)
fig.add_trace(go.Bar(x=["x", "y", "z"], y=[5, 2, 6], name="bar"), row=1, col=2)
fig.update_layout(title_text="Two plots in one figure")
fig.show()Subplots
import plotly.graph_objects as go
from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=["A", "B"])
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9], mode="lines", name="line"), row=1, col=1)
fig.add_trace(go.Bar(x=["x", "y", "z"], y=[5, 2, 6], name="bar"), row=1, col=2)
fig.update_layout(title_text="Two plots in one figure")
fig.show()Tips
- Use facets when youβre comparing the same chart across categories.
- Use subplots when mixing chart types.
If this helped you, consider buying me a coffee β
Buy me a coffeeWas this page helpful?
Let us know how we did
