5 Best Ways to Plot Multiple Figures as Subplots in Python Plotly

πŸ’‘ Problem Formulation: Data visualization often requires the representation of multiple datasets side-by-side for comparison. In Python, using Plotly, one may want to create a single figure containing multiple subplots. This article discusses how to take separate Plotly figures and organize them into subplots within one encompassing figure. The desired output is a cohesive visualization that effectively communicates the relationships between different datasets.

Method 1: Using the make_subplots Function

Plotly’s make_subplots function creates a figure with a grid layout to which you can add traces. This method is useful when you need to customize the layout of your subplots, specifying the number of rows and columns, and controlling their spanning behavior.

Here’s an example:

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

# Create the subplots grid
fig = make_subplots(rows=2, cols=2)

# Add traces
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]), row=1, col=2)
fig.add_trace(go.Bar(x=['a', 'b', 'c'], y=[100, 200, 300]), row=2, col=1)
fig.add_trace(go.Bar(x=['x', 'y', 'z'], y=[-1, -2, -3]), row=2, col=2)

# Show the figure
fig.show()

The code snippet above creates a 2×2 grid of subplots, each containing a different type of Plotly graph. Two scatter plots are placed at the top and two bar charts at the bottom.

Method 2: Using the update_layout Method

The update_layout method allows for the adjustment of subplot properties after they have been created. This includes defining titles and axes labels, among other customization options, to polish the presentation of your subplots.

Here’s an example:

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

fig = make_subplots(rows=1, cols=2)

# Add traces
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=['a', 'b', 'c'], y=[100, 200, 300]), row=1, col=2)

# Update the layout
fig.update_layout(
    title_text='Subplots Example',
    subplot_titles=['Scatter Plot', 'Bar Chart']
)

# Show the figure
fig.show()

In this code snippet, a figure with one row and two columns is created. Different types of plots are then added into each section. The figure’s layout is updated to include a title and subtitles for each subplot.

Method 3: Adding Shared Axis to Subplots

For subplots that share a common axis, Plotly allows you to link these axes with shared_xaxes and shared_yaxes arguments. This can improve readability by enabling users to compare different subplots against the same scale.

Here’s an example:

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

# Create the subplots with shared x-axis
fig = make_subplots(rows=2, cols=1, shared_xaxes=True)

# Add traces
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[6, 5, 4]), row=2, col=1)

# Show the figure
fig.show()

This example demonstrates the creation of vertically arranged subplots that share the same x-axis. This enables direct comparison of Scatter plot data trends across both subplots with ease.

Method 4: Nested Subplots

Plotly also supports nesting subplots where a subplot can contain another set of subplots. This is particularly helpful when dealing with complex data visualization tasks that require hierarchical data presentation.

Here’s an example:

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

# Create the nested subplots
outer_fig = make_subplots(rows=1, cols=2)

# First subplot
inner_fig1 = make_subplots(rows=2, cols=1)
inner_fig1.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
inner_fig1.add_trace(go.Scatter(x=[20, 30, 40], y=[50, 60, 70]), row=2, col=1)

# Second subplot
inner_fig2 = make_subplots(rows=2, cols=1)
inner_fig2.add_trace(go.Bar(x=['a', 'b', 'c'], y=[100, 200, 300]), row=1, col=1)
inner_fig2.add_trace(go.Bar(x=['x', 'y', 'z'], y=[-1, -2, -3]), row=2, col=1)

# Nesting the plots
outer_fig.add_traces(inner_fig1.data, rows=[1]*len(inner_fig1.data), cols=[1]*len(inner_fig1.data))
outer_fig.add_traces(inner_fig2.data, rows=[1]*len(inner_fig2.data), cols=[2]*len(inner_fig2.data))

# Show the figure
outer_fig.show()

The code creates two separate subplot grids and nests them into another subplot. This nesting approach allows for more complex subplot arrangements and can show hierarchical relationships between data.

Bonus One-Liner Method 5: Using Plotly Express

For simplicity, Plotly Express offers an efficient and concise way to create multiple subplots with the make_subplots utility, but utilizing a higher level of abstraction and with fewer lines of code.

Here’s an example:

import plotly.express as px

fig = px.scatter(x=[1, 2, 3], y=[4, 5, 6]).update_traces(showlegend=False).add_traces(
    px.bar(x=['a', 'b', 'c'], y=[100, 200, 300]).data
)
fig.show()

The code snippet uses Plotly Express’s chaining methods to create a combined figure with a scatter and bar plot, thus reducing the code complexity for simpler subplot tasks.

Summary/Discussion

  • Method 1: Using the make_subplots Function. Offers granular control over subplot layout and placement. Can be more verbose for complex layouts.
  • Method 2: Using the update_layout Method. Useful for adding aesthetic enhancements and annotations after subplot creation. May not be necessary for quick and simple visualizations.
  • Method 3: Adding Shared Axis to Subplots. Ensures consistent scaling for comparative analysis. Limited to shared axis scenarios.
  • Method 4: Nested Subplots. Ideal for representing complex hierarchical data structures. Can become unwieldy with too many nested levels.
  • Bonus Method 5: Using Plotly Express. Provides a quick and concise way to create subplots. Lacks the customization possibilities of Plotly’s Graph Objects module.