在 ScatterPlot 中为 hide/show 个数据点创建一个 Plotly 按钮
Create a Plotly button to hide/show data points in ScatterPlot
我显示了一个很大的散点图,现在我想让我的用户能够通过几个按钮过滤掉一些点
例如,假设 points
是一个数组
- button_1 : 只显示点[[1,2,3]]
- button_2 : 只显示
点[[4,5,6]]
有没有简单的方法可以做到这一点?我在网上找了很久...
我知道下面的代码应该可以工作,但我找不到正确的参数:/我想使用一个布尔数组来说明是否应该显示该点
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()
我显示了一个很大的散点图,现在我想让我的用户能够通过几个按钮过滤掉一些点
例如,假设 points
是一个数组
- button_1 : 只显示点[[1,2,3]]
- button_2 : 只显示 点[[4,5,6]]
有没有简单的方法可以做到这一点?我在网上找了很久...
我知道下面的代码应该可以工作,但我找不到正确的参数:/我想使用一个布尔数组来说明是否应该显示该点
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()