我显示了一个大的散点图,现在我想让我的用户能够通过几个按钮过滤掉一些点
例如,假设 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()
关于python - 创建一个 Plotly 按钮以在 ScatterPlot 中隐藏/显示数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69933698/