How to Set up Multiple Subplots with Group Legends using Plotly in Python

Multiple subplots allow you to display several independent plots within a single figure for easy comparison. Plotly provides powerful tools to create subplots with shared legends, making data visualization more organized and informative.

Key Functions

make_subplots()

Creates a grid layout for multiple plots ?

from plotly.subplots import make_subplots

fig = make_subplots(rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4"))

add_trace()

Adds individual plot traces to specific subplot positions ?

import plotly.graph_objects as go

fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="Data Series"), row=1, col=1)

Basic Multiple Subplots Example

Here's how to create a 2x2 subplot grid with shared legends ?

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create subplots with shared legend
fig = make_subplots(rows=2, cols=2, subplot_titles=("Sales Q1", "Sales Q2", "Sales Q3", "Sales Q4"))

# Add traces to different subplots
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[10, 15, 12], name="Product A"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[8, 12, 16], name="Product B"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[12, 18, 14], name="Product A"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[14, 10, 18], name="Product B"), row=2, col=2)

# Update layout
fig.update_layout(width=800, height=600, title="Quarterly Sales Data")

# Display the plot
fig.show()

Customizing Legend Grouping

You can control legend spacing and grouping using tracegroupgap ?

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=("Revenue", "Profit"))

# Add multiple traces
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[20, 25, 30, 28], name="Company A"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[15, 18, 22, 24], name="Company B"), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[5, 7, 8, 4], name="Company A"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3, 4], y=[3, 5, 6, 8], name="Company B"), row=1, col=2)

# Customize legend grouping
fig.update_layout(
    width=800, 
    height=400,
    legend=dict(tracegroupgap=50),
    title="Financial Performance Comparison"
)

fig.show()

Advanced Subplot Configuration

Create more complex layouts with different chart types ?

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create subplots with different chart types
fig = make_subplots(
    rows=2, cols=2,
    subplot_titles=("Scatter Plot", "Bar Chart", "Line Chart", "Area Chart"),
    specs=[[{"type": "scatter"}, {"type": "bar"}],
           [{"type": "scatter"}, {"type": "scatter"}]]
)

# Add different trace types
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="Scatter"), row=1, col=1)
fig.add_trace(go.Bar(x=['A', 'B', 'C'], y=[1, 3, 2], name="Bar"), row=1, col=2)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 4, 3], mode='lines', name="Line"), row=2, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 2, 3], fill='tozeroy', name="Area"), row=2, col=2)

# Update layout
fig.update_layout(
    width=800, 
    height=600,
    showlegend=True,
    title="Multiple Chart Types with Shared Legend"
)

fig.show()

Key Parameters

Parameter Function Description
rows, cols make_subplots() Define grid dimensions
subplot_titles make_subplots() Individual subplot titles
row, col add_trace() Target subplot position
tracegroupgap update_layout() Spacing between legend groups

Conclusion

Plotly's subplot functionality with make_subplots() and add_trace() enables creation of complex multi-panel visualizations with organized legends. This approach is essential for comparing multiple datasets and creating comprehensive data dashboards.

Updated on: 2026-03-27T12:14:08+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements