使用按钮隐藏子图中的标记

问题描述 投票:0回答:1

我想隐藏子图中的标记。

这可能吗?

MRE:

import plotly.graph_objs as go
from plotly.subplots import make_subplots

import numpy as np
np.random.seed(42)
n = 100
x, y, z = np.random.rand(3, n)

fig = make_subplots(rows=1, cols=2, 
                    specs=[[{'type': 'scatter'}, {'type': 'scatter'}]]
                    )

# Scatter plot with markers
fig.add_trace(
go.Scatter(
    x=x,
    y=y,
    mode='lines+markers',
    marker=dict(
        size=5,
        color='red',
        opacity=0.8
    ),
    name='m'
),
row=1, col=1)

fig.add_trace(
 go.Scatter(
    x=x,
    y=y,
    mode='lines+markers',
    marker=dict(
        size=5,
        color='red',
        opacity=0.8
    ),
    name='m2'
), row=1, col=2)



button = dict(
    method='restyle',
    args=[{"mode": "lines+markers", 'marker.size': 0}],  
    label='Remove Markers'
)

fig.update_layout(
    updatemenus=[{
        'buttons': [button],
        'direction': 'down',
        'showactive': True,
        'x': 0.1,
        'xanchor': 'left',
        'y': 1.1,
        'yanchor': 'top'
    }]
)

# Show the figure
fig.show()

在我的机器上,这会从图例中删除标记,但不会从图中删除标记。我究竟做错了什么? 如何仅删除左侧子图上的标记并保留右侧子图上的标记?

python plotly plotly-python
1个回答
0
投票

您只需切换绘图即可

mode
:

button1 = dict(
    method='restyle',
    args=[{"mode": "lines+markers"}],
    label='Show Markers'
)

button2 = dict(
    method='restyle',
    args=[{"mode": "lines"}],
    label='Remove Markers'
)

fig.update_layout(
    updatemenus=[{
        'buttons': [button1, button2],
        'direction': 'down',
        'showactive': True,
        'x': 0.1,
        'xanchor': 'left',
        'y': 1.1,
        'yanchor': 'top'
    }]
)

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.