具有依赖交互的绘图按钮(Python)

2024-04-24 23:46:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图构建一个绘图三维散射图,其中包含大量的记录道和几组按钮,这些按钮决定哪些轨迹子集可见。我想让按钮的互动方式,这取决于其他按钮的状态。例如,假设按钮1和按钮2属于一个按钮栏,按钮A和按钮B属于另一个按钮栏,并且每个栏中一次只能按下一个按钮。我想在按钮1处于活动状态时按下按钮A以显示与在按钮2处于活动状态时按按钮A不同的轨迹子集。在

我在下面列出了一个玩具的例子。它有一个由三个轨迹和两个按钮栏组成的数据集,但是每个按钮的行为目前独立于其他按钮。例如,我希望按钮的行为方式独立

  • 1和按下:显示跟踪1
  • 按下1和B:显示轨迹1和2
  • 2和A按下:显示跟踪3
  • 按下2和B键:显示所有记录道
from plotly.offline import plot
import plotly.plotly as py
import plotly.graph_objs as go
import numpy as np

x, y, z = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 20).transpose()
trace1 = go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        color='rgb(124, 252, 0)',
        size=12,
        opacity=0.8
    )
)

x2, y2, z2 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 20).transpose()
trace2 = go.Scatter3d(
    x=x2,
    y=y2,
    z=z2,
    mode='markers',
    marker=dict(
        color='rgb(30, 144, 255)',
        size=12,
        opacity=0.8
    )
)

x3, y3, z3 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 20).transpose()
trace3 = go.Scatter3d(
    x=x3,
    y=y3,
    z=z3,
    mode='markers',
    marker=dict(
        color='rgb(220, 20, 60)',
        size=12,
        opacity=0.8
    )
)

data = [trace1, trace2, trace3]

layout = go.Layout(
    margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    )
)

button_layer_1_height = 1.12
button_layer_2_height = 1.065

updatemenus=list([
    dict(
        buttons=list([
            dict(
                args=[{'visible': [True, False, True]}],
                label='Button 1',
                method='restyle'
            ),
            dict(
                args=[{'visible': [True, True, False]}],
                label='Button 2',
                method='restyle'
            ),
        ]),
        direction = 'left',
        pad = {'r': 10, 't': 10},
        showactive = True,
        type = 'buttons',
        x = 0.1,
        xanchor = 'left',
        y = button_layer_1_height,
        yanchor = 'top'
    ),
    dict(
        buttons=list([
            dict(
                args=[{'visible': [False, True, False]}],
                label='Button A',
                method='restyle'
            ),
            dict(
                args=['visible', [True, False, False]],
                label='Button B',
                method='restyle'
            )
        ]),
        direction = 'left',
        pad = {'r': 10, 't': 10},
        showactive = True,
        type = 'buttons',
        x = 0.1,
        xanchor = 'left',
        y = button_layer_2_height,
        yanchor = 'top'
    ),
])

layout['updatemenus']=updatemenus
fig = go.Figure(data=data, layout=layout)
plot(fig, filename='localtest.html')

Tags: importlayerfalsetruego轨迹npargs