【问题标题】:Plotly custom buttons overflowPlotly 自定义按钮溢出
【发布时间】:2021-06-23 18:44:26
【问题描述】:

我正在关注这个 Plotly 教程:https://plotly.com/python/custom-buttons/#restyle-button 具体来说,考虑代码:

import plotly.graph_objects as go

import pandas as pd

# load dataset
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")

# create figure
fig = go.Figure()

# Add surface trace
fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))

# Update plot sizing
fig.update_layout(
    width=800,
    height=900,
    autosize=False,
    margin=dict(t=0, b=0, l=0, r=0),
    template="plotly_white",
)

# Update 3D scene options
fig.update_scenes(
    aspectratio=dict(x=1, y=1, z=0.7),
    aspectmode="manual"
)

# Add dropdown
fig.update_layout(
    updatemenus=[
        dict(
            type = "buttons",
            direction = "left",
            buttons=list([
                dict(
                    args=["type", "surface"],
                    label="3D Surface",
                    method="restyle"
                ),
                dict(
                    args=["type", "heatmap"],
                    label="Heatmap",
                    method="restyle"
                )
            ]),
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.11,
            xanchor="left",
            y=1.1,
            yanchor="top"
        ),
    ]
)

# Add annotation
fig.update_layout(
    annotations=[
        dict(text="Trace type:", showarrow=False,
                             x=0, y=1.08, yref="paper", align="left")
    ]
)

fig.show()

现在再添加 15 个按钮,只需添加

        dict(
            args=["type", "surface"],
            label="3D Surface",
            method="restyle"
        )

updatemenusbuttons 中(基本上同一个按钮再添加15 次)。如果你现在运行它,你会看到右边的按钮开始溢出。我该如何解决这个问题?对我来说,显示按钮行是一个很好的解决方案,但显示一个没有溢出的按钮也可以。

【问题讨论】:

    标签: python plot plotly plotly-dash plotly-python


    【解决方案1】:

    这不是最通用的解决方案,但您可以将相同按钮(具有 y 坐标偏移)的单独字典传递给您定义按钮的 fig.update_layoutupdatemenus 参数。

    我不得不反复试验来确定一行可以容纳多少个按钮,以及什么间距看起来最好。理想情况下,可以通过某种方式访问​​按钮的大小,其单位与传递给参数xy 的单位相同。

    import plotly.graph_objects as go
    
    import pandas as pd
    
    # load dataset
    df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/volcano.csv")
    
    # create figure
    fig = go.Figure()
    
    # Add surface trace
    fig.add_trace(go.Surface(z=df.values.tolist(), colorscale="Viridis"))
    
    # Update plot sizing
    fig.update_layout(
        width=800,
        height=900,
        autosize=False,
        margin=dict(t=0, b=0, l=0, r=0),
        template="plotly_white",
    )
    
    # Update 3D scene options
    fig.update_scenes(
        aspectratio=dict(x=1, y=1, z=0.7),
        aspectmode="manual"
    )
    
    # Add dropdown
    fig.update_layout(
        updatemenus=[
            dict(
                type = "buttons",
                direction = "left",
                buttons=list([
                    dict(
                        args=["type", "surface"],
                        label="3D Surface",
                        method="restyle"
                    ),
                    dict(
                        args=["type", "heatmap"],
                        label="Heatmap",
                        method="restyle"
                    )
                ]*4),
                pad={"r": 10, "t": 10},
                showactive=True,
                x=0.11,
                xanchor="left",
                y=1.1,
                yanchor="top"
            ),
    
            ## offset the y coordinate in these buttons
            dict(
                type = "buttons",
                direction = "left",
                buttons=list([
                    dict(
                        args=["type", "surface"],
                        label="3D Surface",
                        method="restyle"
                    ),
                    dict(
                        args=["type", "heatmap"],
                        label="Heatmap",
                        method="restyle"
                    )
                ]*4),
                pad={"r": 10, "t": 10},
                showactive=True,
                x=0.11,
                xanchor="left",
                y=1.15,
                yanchor="top"
            )
        ]
    )
    
    # Add annotation
    fig.update_layout(
        annotations=[
            dict(text="Trace type:", showarrow=False,
                                 x=0, y=1.1, yref="paper", align="left")
        ]
    )
    
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多