When creating plots, especially scatter plots or scientific visualizations, it’s often crucial to ensure that the x and y axes have the same scale. This means that a unit of distance on the x-axis represents the same magnitude as a unit of distance on the y-axis. In Plotly, this is achieved by setting the aspect ratio of the plot. See how to set the same scale for both axes in Plotly.
Understanding Aspect Ratio and Scaling
By default, Plotly (like many plotting libraries) might adjust the aspect ratio of your plot to fill the available space, which can distort the visual representation of your data if equal scaling is important. To enforce the same scale, you need to tell Plotly to maintain an “equal” aspect ratio.
1. Using scaleanchor and scaleratio (Recommended for 2D Plots)
This is the most robust and recommended method for achieving equal scaling on 2D Cartesian plots. You link one axis to another using scaleanchor and then specify their relative scaling with scaleratio.
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[0, 1, 2, 3], y=[0, 1, 2, 3], mode='lines+markers'))
fig.update_layout(
title_text="Equal Scale X and Y Axis",
xaxis=dict(
scaleanchor="y", # Anchor x-axis scale to y-axis
scaleratio=1 # Ensure x-axis units are 1:1 with y-axis units
),
yaxis=dict(
# You can optionally set the range here if needed
# range=[0, 4]
)
)
fig.show()
In this example:
xaxis=dict(scaleanchor="y")
: This tells Plotly that the scaling of the x-axis should be linked to the y-axis.scaleratio=1
: This specifies that one unit on the x-axis should correspond to one unit on the y-axis. If you wanted the x-axis to be twice as stretched as the y-axis, you’d use scaleratio=2.
2. Using constrain and constraintoward (Alternative for 2D Plots)
Another approach for 2D plots is to use constrain and constraintoward on the axis properties. This method is also effective.
import plotly.graph_objects as go
fig = go.Figure(data=go.Scatter(x=[0, 1, 2, 3], y=[0, 1, 2, 3], mode='lines+markers'))
fig.update_layout(
title_text="Equal Scale X and Y Axis (constrain)",
xaxis=dict(
constrain="domain", # Constrain the axis to its domain
constraintoward="middle" # Constrain towards the middle of the domain
),
yaxis=dict(
scaleanchor="x", # Anchor y-axis scale to x-axis
scaleratio=1 # Ensure y-axis units are 1:1 with x-axis units
)
)
fig.show()
While constrain and constraintoward can influence axis behavior, the scaleanchor and scaleratio pair is generally more direct and intuitive for enforcing equal scaling between axes.
3. For 3D Plots (scene layout)
For 3D scatter plots or surface plots, you set the aspect ratio within the scene layout property.
import plotly.graph_objects as go
import numpy as np
# Create some 3D data
x = np.random.rand(10)
y = np.random.rand(10)
z = np.random.rand(10)
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')])
fig.update_layout(
title_text="Equal Scale in 3D Plot",
scene=dict(
xaxis_title='X Axis',
yaxis_title='Y Axis',
zaxis_title='Z Axis',
aspectmode='data' # This sets the aspect ratio to match the data's scale
)
)
fig.show()
In 3D plots, setting aspectmode=’data’ within the scene dictionary ensures that the aspect ratio of the axes corresponds to the data ranges, making units on all axes visually comparable.
Key Points
- For 2D Cartesian plots, the most effective method is to use
scaleanchor="y"
(or “x”) andscaleratio=1
on one of the axes. - For 3D plots, set
aspectmode='data'
within thescene
layout property. - Manually setting axis ranges (e.g., xaxis_range=[min, max]) can sometimes interfere with automatic scaling, so use them carefully in conjunction with equal scaling.