python - 创建一个 Plotly 按钮以在 ScatterPlot 中隐藏/显示数据点

标签 python plotly

我显示了一个大的散点图,现在我想让我的用户能够通过几个按钮过滤掉一些点

例如,假设 points 是一个数组

  • button_1:只显示点[[1,2,3]]
  • button_2 :仅显示 点[[4,5,6]]

有没有简单的方法可以做到这一点?我在网上找了很久...

我知道下面的代码应该可以工作,但我找不到正确的参数:/我想使用一个 bool 数组来说明是否应该显示该点

fig.update_layout(
    updatemenus=[
        dict(
            type="buttons",
            buttons=[
                dict(method='restyle',
                        label=col,
                        visible=True,
                        args=< ??? >,
                        ),
            ],
        )
    ]
)

最佳答案

我认为传递诸如 args={'visible': [True, False]} 之类的字典是您要寻找的。具有这些设置的按钮将显示第一条轨迹并隐藏第二条轨迹,因此只要您使用多条轨迹创建散点图,这就会起作用。例如:

import pandas as pd
import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(
    x=[1,2,2],
    y=[4,5,4],
    mode='markers',
    name='cluster 1',
    marker=dict(color="red")
))

fig.add_trace(go.Scatter(
    x=[7,8,7],
    y=[3,4,2],
    mode='markers',
    name='cluster 2',
    marker=dict(color="blue")
))

fig.update_layout(
    updatemenus=[
        dict(
         buttons=list([   
            dict(label = 'show cluster 1',
                 method = 'update',
                 args = [{'visible': [True, False]},
                         {'title': 'cluster 1'}]),

            dict(label = 'show cluster 2',
                 method = 'update',
                 args = [{'visible': [False, True]},
                         {'title': 'cluster 2'}]),
            
            dict(label = 'show both clusters',
                 method = 'update',
                 args = [{'visible': [True, True]},
                         {'title': 'cluster 2'}])
        ]),
    )]
)

fig.update_xaxes(range=[0, 10])
fig.update_yaxes(range=[0, 10])
fig.show()

enter image description here

关于python - 创建一个 Plotly 按钮以在 ScatterPlot 中隐藏/显示数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69933698/

相关文章:

plotly - 如何使用 plotly express 添加置信区间 fillcontour?

python - Blender 2.5 Python 动画世界纹理设置

python - Pandas pivot_table 的更快替代品

python - 将 plot.ly 用于 Python 时,Module_six_moves_urllib_parse 对象没有属性 urlparse

R 绘图 + Shiny 的 react 耦合事件 - 通过单击同一图表来刷新带有参数的图表

r - 注释的字体大小

plotly - 添加 Plotly Modebar 按钮,允许切换悬停模式

python - 如何定义 Google Endpoints API 文件下载消息端点

python - 如何从文件中解析 Python 模块

python - 运行单元测试时没有弃用警告