How to apply different titles for each different subplots using Plotly in Python?

Subplot creation is one of several tools for data visualization provided by the Python library Plotly. A big narrative can be broken up into multiple smaller ones using subplots. Sometimes, in order to give the main story greater depth and consistency, it may be essential to give each subplot its own title.

Syntax

Customizing subplot titles in plot grids is made possible through the usage of the subplot_titles parameter, which enables us to create unique titles for each plot. The make_subplots() function is essentially a factory method that allows us to establish a plot grid with a designated number of rows and columns.

from plotly.subplots import make_subplots

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

Key Parameters

Let's delve into some of the key parameters we can manipulate with make_subplots() ?

  • rows ? This parameter specifies the number of rows in the plot grid.

  • cols ? This parameter specifies the number of columns in the plot grid.

  • specs ? An array of arrays that delineates the type of each subplot in the grid. Each element in the specs array should comprise two values: the number of rows and columns the subplot spans, respectively, and the subplot type.

  • subplot_titles ? An array of strings that illustrates the titles of each subplot in the grid. The size of this array should equal the number of subplots in the grid.

Example: Trigonometric Functions Subplots

Let's create three subplots showing different trigonometric functions with individual titles ?

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

# Create data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.tan(x)

# Create subplots with titles
fig = make_subplots(
    rows=1, 
    cols=3, 
    subplot_titles=("Sin(x)", "Cos(x)", "Tan(x)")
)

# Add traces
fig.add_trace(go.Scatter(x=x, y=y1, name='Sin(x)'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=y2, name='Cos(x)'), row=1, col=2)
fig.add_trace(go.Scatter(x=x, y=y3, name='Tan(x)'), row=1, col=3)

# Update layout and axes
fig.update_layout(title_text="Trigonometric Functions")
fig.update_xaxes(title_text="X values", row=1, col=1)
fig.update_xaxes(title_text="X values", row=1, col=2)
fig.update_xaxes(title_text="X values", row=1, col=3)
fig.update_yaxes(title_text="Sin values", row=1, col=1)
fig.update_yaxes(title_text="Cos values", row=1, col=2)
fig.update_yaxes(title_text="Tan values", row=1, col=3)

# Display the plot
fig.show()

Example: Multiple Rows with Different Titles

You can also create subplots with multiple rows, each having unique titles ?

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

# Create sample data
x = np.linspace(0, 10, 50)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = x**2
y4 = np.log(x + 1)

# Create 2x2 subplot grid with titles
fig = make_subplots(
    rows=2, 
    cols=2,
    subplot_titles=("Sine Wave", "Cosine Wave", "Quadratic Function", "Logarithmic Function")
)

# Add traces to each subplot
fig.add_trace(go.Scatter(x=x, y=y1, name='sin(x)'), row=1, col=1)
fig.add_trace(go.Scatter(x=x, y=y2, name='cos(x)'), row=1, col=2)
fig.add_trace(go.Scatter(x=x, y=y3, name='x²'), row=2, col=1)
fig.add_trace(go.Scatter(x=x, y=y4, name='log(x+1)'), row=2, col=2)

# Update layout
fig.update_layout(
    title_text="Mathematical Functions Dashboard",
    showlegend=False,
    height=600
)

fig.show()

How It Works

The process involves these key steps:

  1. Grid Creation ? Use make_subplots() to define the subplot grid structure with rows and columns.

  2. Title Assignment ? Pass a tuple or list of titles to the subplot_titles parameter. The titles are applied in order from left to right, top to bottom.

  3. Adding Data ? Use add_trace() with row and col parameters to specify which subplot receives each trace.

  4. Customization ? Use update_xaxes() and update_yaxes() to customize individual subplot axes.

Conclusion

Plotly's make_subplots() function provides a convenient way to create subplot grids with individual titles using the subplot_titles parameter. You can further customize each subplot's axes and appearance using the update methods, making it perfect for creating comprehensive data visualization dashboards.

Updated on: 2026-03-27T00:43:37+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements