plotly custom buttons: is it possible to set the same scale for multiple subplots?

岁酱吖の 提交于 2021-01-28 14:09:26

问题


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:


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()


来源:https://stackoverflow.com/questions/62937509/plotly-custom-buttons-is-it-possible-to-set-the-same-scale-for-multiple-subplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!