python - 具有相关交互的 Plotly 按钮 (Python)
问题描述
我正在尝试构建一个带有大量迹线和几组按钮的 Plotly 3D 散点图,这些按钮确定哪些迹线子集是可见的。我希望按钮以取决于其他按钮状态的方式进行交互。例如,假设 Button 1 和 Button 2 属于一个按钮栏,而 Button A 和 Button B 属于另一栏,并且每个 bar 中一次只能按下一个按钮。我想在按钮 1 处于活动状态时按按钮 A 以显示与在按钮 2 处于活动状态时按按钮 A 不同的跟踪子集。
我在下面包含了一个玩具示例。它有一个包含三个轨迹和两个按钮栏的数据集,但每个按钮的行为当前独立于其他按钮。我希望按钮以依赖的方式运行,例如,
- 1 和 A 按下:显示迹线 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')
解决方案
我在以下教程中找到了这个问题的答案:https ://dash.plot.ly/getting-started-part-2 。我使用单选项目而不是按钮构建了一个 Dash 应用程序,结果效果很好。
推荐阅读
- php - PHP后期静态绑定(new static):如何保证子类构造函数兼容性和处理发散构造函数?
- unit-testing - Angular 6 业力测试
- python - Python - readline() 循环将所有行放入字典中的相同值槽中
- powershell - PowerShell 中的动态相关参数
- c# - ASP.NET Core 中的 StructureMap 未按预期处理基元
- sql - 使用本地日期表示法访问日期范围之间的 sql 查询
- c# - 如何在 Visual Studio 中构建版本到版本子文件夹
- c# - 如何在 Entity Framework 6.2 中添加外键自定义名称?
- networking - 将 Kubernetes 服务公开为 NodePort 后无法连接到它
- bash - 远程服务器可用时运行命令的 Bash 脚本