Open In App

Enhancing Subplots in Plotly: Adding Borders, Sidelabels, and Synchronizing Panning

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

With the help of the flexible data visualization framework Plotly, users may create dynamic and eye-catching charts. It is particularly effective in developing subplots, which are many storylines organized within one figure. To enhance the user experience while dealing with subplots, borders, side labels, and synchronized panning may be necessary. Borders should also be included for greater visual distinction. This tutorial will walk you through adding borders and side labels to your subplots and show you how to use Plotly in Python to synchronize panning between subplots.

Creating Subplots in Plotly

Before diving into adding borders and other enhancements, let's first understand how to create subplots in Plotly. Subplots are useful for displaying multiple plots in a single figure, allowing for easy comparison and analysis.

You can refer to the link for understanding how to: Create Subplots in Plotly

Adding Borders to Subplots

Subplot borders may aid in the visual differentiation of each subplot, facilitating user interpretation of the data. Although adding borders is not possible directly in Plotly, it may be done by modifying the layout parameters.

The following is how borders are added to subplots:

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

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

# Add traces
fig.add_trace(go.Scatter(y=[1, 3, 2], mode='lines'), row=1, col=1)
fig.add_trace(go.Scatter(y=[4, 1, 3], mode='lines'), row=1, col=2)
fig.add_trace(go.Scatter(y=[5, 6, 4], mode='lines'), row=2, col=1)
fig.add_trace(go.Scatter(y=[7, 8, 6], mode='lines'), row=2, col=2)

# Update layout for borders
fig.update_layout(
    xaxis=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis2=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis2=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis3=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis3=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis4=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis4=dict(showline=True, linewidth=2, linecolor='black', mirror=True)
)

fig.show()

Output:

Screenshot

Adding Sidelabels to Subplots

By adding further context to your subplots, side labels help the viewer better comprehend the data being shown. You may use annotations in the Plotly layout to create sidelabels. Custom labeling is possible with the ability to arrange annotations in relation to individual subplots.

This is how sidelabels are added:

Python
# Add sidelabels
fig.update_layout(
    annotations=[
        dict(text="Label 1", xref="paper", yref="paper", x=0.25, y=1.05, showarrow=False),
        dict(text="Label 2", xref="paper", yref="paper", x=0.75, y=1.05, showarrow=False),
        dict(text="Label 3", xref="paper", yref="paper", x=0.25, y=-0.1, showarrow=False),
        dict(text="Label 4", xref="paper", yref="paper", x=0.75, y=-0.1, showarrow=False)
    ]
)

fig.show()

Output:

Screenshot-

Synchronizing Panning Across Subplots

By guaranteeing that panning in one subplot immediately modifies the display in the other subplots, synchronizing panning across subplots enables users to examine the data with ease. This capability comes in handy especially when comparing datasets that have similar axes.

You may connect the axes together and use the relayout approach to synchronize panning:

Python
# Link x-axes for synchronized panning
fig.update_xaxes(matches='x')

# Link y-axes for synchronized panning
fig.update_yaxes(matches='y')

fig.show()

Output:

file
Synchronizing Panning Across Subplots

Creating Subplots with Synchronized Axes and Custom Borders

To see these techniques in action, let’s combine everything into a single example:

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

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

# Add traces
fig.add_trace(go.Scatter(y=[1, 3, 2], mode='lines'), row=1, col=1)
fig.add_trace(go.Scatter(y=[4, 1, 3], mode='lines'), row=1, col=2)
fig.add_trace(go.Scatter(y=[5, 6, 4], mode='lines'), row=2, col=1)
fig.add_trace(go.Scatter(y=[7, 8, 6], mode='lines'), row=2, col=2)

# Update layout for borders
fig.update_layout(
    xaxis=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis2=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis2=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis3=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis3=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    xaxis4=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    yaxis4=dict(showline=True, linewidth=2, linecolor='black', mirror=True),
    annotations=[
        dict(text="Label 1", xref="paper", yref="paper", x=0.25, y=1.05, showarrow=False),
        dict(text="Label 2", xref="paper", yref="paper", x=0.75, y=1.05, showarrow=False),
        dict(text="Label 3", xref="paper", yref="paper", x=0.25, y=-0.1, showarrow=False),
        dict(text="Label 4", xref="paper", yref="paper", x=0.75, y=-0.1, showarrow=False)
    ]
)

# Link x-axes and y-axes for synchronized panning
fig.update_xaxes(matches='x')
fig.update_yaxes(matches='y')

fig.show()

Output:

file

Conclusion

Plotly provides a robust framework for creating interactive and customizable visualizations. By utilizing features such as borders, sidelabels, and synchronized panning, you can enhance the readability and functionality of your plots. These techniques are especially useful when dealing with complex datasets that require multiple visual representations for comprehensive analysis.


Explore