Thanks to visit codestin.com
Credit goes to github.com

Skip to content

update_yaxes behavior for make_subplots vs specifying axes directly #5213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
RSDummy0 opened this issue Jun 2, 2025 · 3 comments
Open

Comments

@RSDummy0
Copy link

RSDummy0 commented Jun 2, 2025

--copied from the plotly forum here https://community.plotly.com/t/update-yaxes-behavior-when-using-make-subplots-vs-specifying-axes-directly/92496--

To my knowledge, there’s two main ways to create subplots:

make_subplots(), then add trace data - example here
create list of data, inject into go.Figure() - example here (see hover on subplots)

I’m not sure which one’s the preferred one, but update_yaxes affects them differently.

If we use method 1, the update_yaxes affects all yaxes as intended
If we use method 2, the update_yaxes only affects the first subplot

Is there a reason why update_yaxes doesn’t play well with the second way of creating subplots?

@RSDummy0
Copy link
Author

RSDummy0 commented Jun 2, 2025

example code for method 1 - using subplots, update_yaxes modifies all three axes

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

df = data.stocks()

fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.03)
layout = dict(
hoversubplots="axis",
title="Stock Price Changes",
hovermode="x",
grid=dict(rows=3, columns=1),
)

fig.add_trace(go.Scatter(x=df["date"], y=df["AAPL"], name="Apple"), row=1, col=1)
fig.add_trace(go.Scatter(x=df["date"], y=df["GOOG"], name="Google"), row=2, col=1)
fig.add_trace(go.Scatter(x=df["date"], y=df["AMZN"], name="Amazon"), row=3, col=1)
fig.update_layout(layout)
fig.update_yaxes(dict(rangemode='tozero'))

fig.show()

Image

@RSDummy0
Copy link
Author

RSDummy0 commented Jun 2, 2025

example code for method 2 - specifying axis directly, update_yaxes only modifies the behavior of the first subplot

import plotly.graph_objects as go
import pandas as pd
from plotly import data

df = data.stocks()

layout = dict(
hoversubplots="axis",
title=dict(text="Stock Price Changes"),
hovermode="x",
grid=dict(rows=3, columns=1),
)

data = [
go.Scatter(x=df["date"], y=df["AAPL"], xaxis="x", yaxis="y", name="Apple"),
go.Scatter(x=df["date"], y=df["GOOG"], xaxis="x", yaxis="y2", name="Google"),
go.Scatter(x=df["date"], y=df["AMZN"], xaxis="x", yaxis="y3", name="Amazon"),
]

fig = go.Figure(data=data, layout=layout)
fig.update_yaxes(dict(rangemode='tozero'))
fig.show()

Image

@RSDummy0
Copy link
Author

RSDummy0 commented Jun 2, 2025

Update_yaxes’ documentation states that the supposed behavior is to modify all y-axes at once, link

“By default, these methods apply to all of the x axes or y axes in the figure. The row and col arguments can be used to control which axes are targeted by the update.”

method 1’s fig.layout:

Image

method 2’s fig.layout:

Image

The function is able to recognise that it needs to modify yaxis, yaxis2, etc, but only when creating subplots via method 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant