update_yaxes behavior for make_subplots vs specifying axes directly
--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?
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()
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()
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:
method 2’s fig.layout:
The function is able to recognise that it needs to modify yaxis, yaxis2, etc, but only when creating subplots via method 1.