绘图自定义按钮:是否可以为多个子图设置相同的比例? [英] plotly custom buttons: is it possible to set the same scale for multiple subplots?

查看:119
本文介绍了绘图自定义按钮:是否可以为多个子图设置相同的比例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在子图中绘制了一些数据.默认情况下,每个子图都会自动缩放.

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.

是否可以使用按钮来做到这一点,样式为 https://plotly.com/python/custom-buttons/

Is it possible to do this with a button, in the style of https://plotly.com/python/custom-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])

推荐答案

在这种情况下,您需要在更改布局时使用 relayout 而不是 update.然后在两个按钮中,您应该为 yaxisyaxis2 定义 autorange: Truerange: [y_min, y_max].

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

这篇关于绘图自定义按钮:是否可以为多个子图设置相同的比例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆