 Align Multiple Y axis to one value in Plotly

I have the following code (slightly modified from Plotly’s page)

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

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

go.Scatter(x=[1, 2, 3], y=[40, 50, 60], name="yaxis data"),
secondary_y=False,
)

go.Scatter(x=[2, 3, 4], y=[80, 40, 30], name="yaxis2 data"),
secondary_y=True,
)

fig.update_layout(
title_text="Double Y Axis Example"
)

# Set x-axis title
fig.update_xaxes(title_text="xaxis title")

# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> yaxis title", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)

fig.show()

This gives me

Now you see the two red circles on on the left and one on the right. You can see that the value 50 is not aligned to the same height.

How can I make that the left Y axis and the right Y axis are aligned at one particular point? (in the majority of cases it would be 0)

Also I would like to clarify that the values in both axis (left and right) can be widely different. For example from -0.01 to 0.01 in the left and from -2 to -3 in the right. However both will have 0 so 0 has to be at the same height

Hi Kansai,

Had a similar problem to yours. I found that by extending both axis ranges to include 0, e.g.,

fig.update_yaxes(rangemode='tozero')

I could align the zeroline of both axes, like so:

Furthermore, constraining the axes to scale with each other causes their values to line up completely:

fig.update_yaxes(rangemode='tozero', scaleanchor='y', scaleratio=1, constraintoward='bottom', secondary_y=True)
fig.update_yaxes(rangemode='tozero', scaleanchor='y2', scaleratio=1, constraintoward='bottom', secondary_y=False)

Hope this helps!