在绘图中,当使用“更新”按钮在不同图形之间切换时如何动态更改轴标题

问题描述 投票:1回答:1

我有3个单独的散点图,可以使用按钮选择,只有因变量在它们之间改变。如何为yaxis_title编码以使每个选择的按钮改变?我没有在他们的教程中找到任何证明。我已附上sample tutorial中的代码作为示例。

import plotly.graph_objects as go

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 = go.Figure()

# Add Traces

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

fig.add_trace(
go.Scatter(x=list(df.index),
           y=[df.High.mean()] * len(df.index),
           name="High Average",
           visible=False,
           line=dict(color="#33CFA5", dash="dash")))

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

fig.add_trace(
go.Scatter(x=list(df.index),
           y=[df.Low.mean()] * len(df.index),
           name="Low Average",
           visible=False,
           line=dict(color="#F06A6A", dash="dash")))

# Add Annotations and Buttons
high_annotations = [dict(x="2016-03-01",
                     y=df.High.mean(),
                     xref="x", yref="y",
                     text="High Average:<br> %.2f" % df.High.mean(),
                     ax=0, ay=-40),
                dict(x=df.High.idxmax(),
                     y=df.High.max(),
                     xref="x", yref="y",
                     text="High Max:<br> %.2f" % df.High.max(),
                     ax=0, ay=-40)]
low_annotations = [dict(x="2015-05-01",
                    y=df.Low.mean(),
                    xref="x", yref="y",
                    text="Low Average:<br> %.2f" % df.Low.mean(),
                    ax=-40, ay=40),
               dict(x=df.High.idxmin(),
                    y=df.Low.min(),
                    xref="x", yref="y",
                    text="Low Min:<br> %.2f" % df.Low.min(),
                    ax=0, ay=40)]

fig.update_layout(
   updatemenus=[
    dict(
        type="buttons",
        direction="right",
        active=0,
        x=0.57,
        y=1.2,
        buttons=list([
            dict(label="None",
                 method="update",
                 args=[{"visible": [True, False, True, False]},
                       {"title": "Yahoo",
                        "annotations": []}]),
            dict(label="High",
                 method="update",
                 args=[{"visible": [True, True, False, False]},
                       {"title": "Yahoo High",
                        "annotations": high_annotations}]),
            dict(label="Low",
                 method="update",
                 args=[{"visible": [False, False, True, True]},
                       {"title": "Yahoo Low",
                        "annotations": low_annotations}]),
            dict(label="Both",
                 method="update",
                 args=[{"visible": [True, True, True, True]},
                       {"title": "Yahoo",
                        "annotations": high_annotations + low_annotations}]),
        ]),
    )
])

# Set title
fig.update_layout(
    title_text="Yahoo",
    xaxis_domain=[0.05, 1.0]
)

fig.show()
python plotly axis-labels
1个回答
0
投票

Changing the xy-Axis Titles with Dropdown Menu这是与您和答案相同的问题。

        buttons=list([
        dict(label="None",
             method="update",
             args=[{"visible": [True, False, True, False]},
                   {"title": "Yahoo",
                    "xaxis": {"title": "Date"},
                    "yaxis": {"title": "High Price - Dollars"},
                    "annotations": []}]),
© www.soinside.com 2019 - 2024. All rights reserved.