Plotly’s subplot functionality is incredibly useful for displaying multiple related plots in a single figure. By default, make_subplots adds a certain amount of spacing between these plots for readability. However, there are many cases where you might want to reduce or completely remove this space to create a more compact or visually continuous layout. See how to control the spacing between subplots in Plotly.
Understanding Subplot Spacing
When you create subplots using plotly.subplots.make_subplots, the spacing is controlled by the horizontal_spacing and vertical_spacing parameters. These parameters accept values between 0 and 1, representing a fraction of the total plot width/height.
1. Controlling Spacing During make_subplots Creation
The most direct way to manage spacing is when you initially create your subplot figure.
Example: Reducing Spacing
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with reduced horizontal and vertical spacing
fig = make_subplots(
rows=2, cols=2,
subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4"),
horizontal_spacing=0.02, # Reduce horizontal space (default is usually around 0.05-0.1)
vertical_spacing=0.02 # Reduce vertical space
)
# Add traces to each subplot
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[5, 6, 7]), row=1, col=2)
fig.add_trace(go.Scatter(x=[3, 4, 5], y=[6, 7, 8]), row=2, col=1)
fig.add_trace(go.Scatter(x=[4, 5, 6], y=[7, 8, 9]), row=2, col=2)
fig.update_layout(title_text="Subplots with Reduced Spacing")
fig.show()
By setting horizontal_spacing and vertical_spacing to a small value like 0.02, you can significantly reduce the gaps between your plots.
Example: Completely Removing Spacing
To completely remove the space, set the spacing parameters to 0.
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with NO horizontal and vertical spacing
fig = make_subplots(
rows=2, cols=2,
subplot_titles=("Plot A", "Plot B", "Plot C", "Plot D"),
horizontal_spacing=0, # No horizontal space
vertical_spacing=0 # No vertical space
)
# Add traces to each subplot
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Scatter(x=[2, 3, 4], y=[5, 6, 7]), row=1, col=2)
fig.add_trace(go.Scatter(x=[3, 4, 5], y=[6, 7, 8]), row=2, col=1)
fig.add_trace(go.Scatter(x=[4, 5, 6], y=[7, 8, 9]), row=2, col=2)
fig.update_layout(title_text="Subplots with No Spacing")
fig.show()
Setting the spacing to 0 makes the plots touch each other, which can be desirable for certain types of visualizations, like heatmaps or image grids.
2. Adjusting Spacing After Figure Creation (using update_layout)
If you’ve already created your subplot figure and want to adjust the spacing, you can do so using fig.update_layout().
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots with default spacing first
fig = make_subplots(
rows=1, cols=2,
subplot_titles=("Left Plot", "Right Plot")
)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[10, 11, 12]), row=1, col=1)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[13, 14, 15]), row=1, col=2)
# Now, update the layout to change spacing
fig.update_layout(
title_text="Adjusted Spacing After Creation",
margin=dict(l=20, r=20, t=50, b=20), # Adjust margins if needed
# Use 'xaxis' and 'yaxis' properties for each subplot's domain
# This is more complex for existing plots and often better handled at make_subplots
# However, you can directly set the 'gap' for the grid layout if you're using 'grid' properties
# For make_subplots, the parameters are directly accessible
# If you need to change after creation, you'd typically re-evaluate the domain of each axis.
# For simplicity, it's best to set during make_subplots or consider axis domains.
# The 'spacing' parameters are part of the 'grid' property of the layout.
grid=dict(
xgap=0.01, # Horizontal gap
ygap=0.01 # Vertical gap
)
)
fig.show()
While make_subplots directly uses horizontal_spacing and vertical_spacing, when modifying an existing layout, you’d typically adjust the xgap and ygap within the grid property of the layout, or more precisely, the domain of each individual axis if you need very specific control over the plot areas. For most cases, setting the spacing during make_subplots is the most straightforward approach.
Considerations When Removing Spacing
- Axis Labels and Titles: When plots touch, axis labels and titles might overlap or become difficult to read. You might need to adjust their visibility or position.
- Subplot Titles: If you use subplot_titles, they will still appear above each plot. Consider removing them or adjusting their font size if plots are very close.
- Visual Continuity: Removing space creates a strong visual continuity, which is excellent for certain types of data (e.g., time series that should appear as one continuous flow).
- Margins: Ensure your overall figure margins (margin in update_layout) are sufficient so that elements like the main title or legend don’t get cut off.