1

I plot some data in subplots. Each subplot is autoscaled by default.

For easy comparison, I sometimes want to have the same scale in all subplots.

Is it possible to do this with a button, in the style of https://plotly.com/python/custom-buttons/

Sample code with buttons:

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

# Load dataset
df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df.columns = [col.replace("AAPL.", "") for col in df.columns]

# Initialize figure

fig = make_subplots(rows=1, cols=2)

# Add Traces

fig.add_trace(
    go.Scatter(x=list(df.index),
               y=list(df.High*2),
               name="High",
               line=dict(color="#33CFA5")),
    row = 1, col = 1
)

fig.add_trace(
    go.Scatter(x=list(df.index),
               y=list(df.Low),
               name="Low",
               line=dict(color="#F06A6A")),
    row = 1, col = 2
)


# Add Buttons

fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            direction="right",
            active=0,
            x=0.57,
            y=1.2,
            buttons=list([
                dict(label="Autoscale for each",
                     method="update",
                     args=[ # set autoscale for each subplot
                           {"title": "Autoscale"}]),
                dict(label="Same scale",
                     method="update",
                     args=[ # set same scale for all. how? 
                           {"title": "Same scale for all"}]),
            ]),
        )
    ])

# Set title
fig.update_layout(
    title_text="Yahoo",
)

fig.show()

P.S. I know how to do this manually:

fig.update_yaxes(range=[ymin, ymax])

1 Answer 1

3

In this case you need to use relayout instead of update as you are changing layout. Then in both buttons you should define autorange: True or range: [y_min, y_max] for yaxis and yaxis2.

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

# Load dataset
df = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
df.columns = [col.replace("AAPL.", "") for col in df.columns]

# you need to define yaxis range
y_max = max(df["High"].max()*2, df["Low"].max())
y_min = min(df["High"].min()*2, df["Low"].min())

# Initialize figure

fig = make_subplots(rows=1, cols=2)

# Add Traces

fig.add_trace(
    go.Scatter(x=df.index,
               y=df["High"]*2,
               name="High",
               line=dict(color="#33CFA5")),
    row = 1, col = 1
)

fig.add_trace(
    go.Scatter(x=df.index,
               y=df["Low"],
               name="Low",
               line=dict(color="#F06A6A")),
    row = 1, col = 2
)


# Add Buttons

fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            direction="right",
            active=0,
            x=0.57,
            y=1.2,
            buttons=list([
                dict(label="Autoscale for each",
                     method="relayout",
                     args=[{'yaxis.autorange': True,
                            'yaxis2.autorange': True},
                          ]),
                dict(label="Same scale",
                     method="relayout",
                     args=[{'yaxis.range': [y_min, y_max],
                            'yaxis2.range': [y_min, y_max]}]),
            ]),
        )
    ])

# Set title
fig.update_layout(
    title_text="Yahoo"
)

fig.show()
Sign up to request clarification or add additional context in comments.

2 Comments

That works! what if the number of subplots is variable? i.e. I don't know beforehand whether I will have 2 subplots, or, say, 12.
In that case you need to add all possible taxis. That is yaxis, yaxis2,..., yaxis12 inside args. In that case it's better to use a loop rather than write them one by one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.