Plotly 添加按钮下拉菜单


Plotly 通过使用绘图区域上的不同控件(例如按钮、下拉列表和滑块等)提供高度的交互性。这些控件与 更新菜单 Plotly 布局的属性。你可以 添加按钮 通过指定要调用的方法及其行为。

有四种可能的方法可以与按钮关联,如下所示:

  • restyle :修改数据或数据属性

  • relayout :修改布局属性

  • update :修改数据和布局属性

  • animate :开始或暂停动画

The restyle 方法应该在什么时候使用 修改数据和数据属性 图的。在以下示例中,添加了两个按钮 更新菜单() 布局方法 restyle method.

go.layout.Updatemenu(
type = "buttons",
direction = "left",
buttons = list([
    dict(args = ["type", "box"], label = "Box", method = "restyle"),
    dict(args = ["type", "violin"], label = "Violin", method = "restyle" )]
))

Value of type 财产是 buttons 默认。要呈现按钮的下拉列表,请将类型更改为 dropdown .在如上更新其布局之前添加到 Figure 对象的 Box 跟踪。呈现的完整代码 boxplot and 小提琴Plotly 取决于点击的按钮,如下:

import plotly.graph_objs as go
fig = go.Figure()
fig.add_trace(go.Box(y = [1140,1460,489,594,502,508,370,200]))
fig.layout.update(
    updatemenus = [
        go.layout.Updatemenu(
            type = "buttons", direction = "left", buttons=list(
                [
                    dict(args = ["type", "box"], label = "Box", method = "restyle"),
                    dict(args = ["type", "violin"], label = "Violin", method = "restyle")
                ]
            ),
            pad = {"r": 2, "t": 2},
            showactive = True,
            x = 0.11,
            xanchor = "left",
            y = 1.1,
            yanchor = "top"
        ),
    ]
)
iplot(fig)

代码输出如下:

Violin Button

Click on Violin 按钮显示对应的 小提琴Plotly .

Dropdown List Button

如上所述,价值 type key in 更新菜单() 方法已分配 dropdown 显示按钮的下拉列表。Plotly 如下图:

Update Method

The update 修改图形的数据和布局部分时应使用方法。以下示例演示了如何更新以及显示哪些轨迹,同时更新布局属性,例如图表标题。两条 Scatter 轨迹对应于 正弦和余弦波 被添加到 图对象 .可见的痕迹 属性 as True 将显示在图上,其他痕迹将被隐藏。

import numpy as np
import math #needed for definition of pi

xpoints = np.arange(0, math.pi*2, 0.05)
y1 = np.sin(xpoints)
y2 = np.cos(xpoints)
fig = go.Figure()
# Add Traces
fig.add_trace(
    go.Scatter(
        x = xpoints, y = y1, name = 'Sine'
    )
)
fig.add_trace(
    go.Scatter(
        x = xpoints, y = y2, name = 'cos'
    )
)
fig.layout.update(
    updatemenus = [
        go.layout.Updatemenu(
            type = "buttons", direction = "right", active = 0, x = 0.1, y = 1.2,
            buttons = list(
                [
                    dict(
                        label = "first", method = "update",
                        args = [{"visible": [True, False]},{"title": "Sine"} ]
                    ),
                    dict(
                        label = "second", method = "update",
                        args = [{"visible": [False, True]},{"title": Cos"}]
                    )
                ]
            )
        )
    ]
)
iplot(fig)

最初, 正弦曲线 将显示。如果点击第二个按钮, cos追踪 appears.

注意 图表标题 也相应地更新。

Sine Curve

为了使用 animate 方法,我们需要添加一个或多个 框架到图 目的。与数据和布局一起,可以将框架添加为图形对象中的键。帧关键点指向一个图形列表,每个图形都会在动画触发时循环播放。

你可以添加、播放和暂停按钮以在图表中添加动画 更新菜单数组 到布局。

"updatemenus": [{
    "type": "buttons", "buttons": [{
        "label": "Your Label", "method": "animate", "args": [frames]
    }]
}]

在以下示例中,一个 散点曲线 首先绘制迹线。然后加 frames 这是一个列表 50 帧对象 ,每个代表一个 红色标记 曲线上。请注意, args 按钮的属性设置为无,因此所有帧都是动画的。

import numpy as np
t = np.linspace(-1, 1, 100)
x = t + t ** 2
y = t - t ** 2
xm = np.min(x) - 1.5
xM = np.max(x) + 1.5
ym = np.min(y) - 1.5
yM = np.max(y) + 1.5
N = 50
s = np.linspace(-1, 1, N)
#s = np.arange(0, math.pi*2, 0.1)
xx = s + s ** 2
yy = s - s ** 2
fig = go.Figure(
    data = [
        go.Scatter(x = x, y = y, mode = "lines", line = dict(width = 2, color = "blue")),
        go.Scatter(x = x, y = y, mode = "lines", line = dict(width = 2, color = "blue"))
    ],
    layout = go.Layout(
        xaxis=dict(range=[xm, xM], autorange=False, zeroline=False),
        yaxis=dict(range=[ym, yM], autorange=False, zeroline=False),
        title_text="Moving marker on curve",
        updatemenus=[
            dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None])])
        ]
    ),
    frames = [go.Frame(
        data = [
                go.Scatter(
                x = [xx[k]], y = [yy[k]], mode = "markers", marker = dict(
                    color = "red", size = 10
                )
            )
        ]
    )
    for k in range(N)]
)
iplot(fig)

代码输出如下:

Play Button

单击时,红色标记将开始沿曲线移动 play button.