Python Plotly Express Line Graph 使用figure.add_selection后删除选择框

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

所以,我有这个应用程序,我在其中使用

plotly.express.line
创建一个图形,然后使用
add_selection() 
函数,如下所示:
figure.add_selection( x0=0, y0=10, x1=10, y1=0 )
确实添加了一个选择框。 但是,问题是我想通过脚本删除这个选择框。我尝试了很多事情,比如
figure.update_layout(dragmode='zoom')
但即使这样也不会删除选择框。有类似
figure.clear_selection()
之类的功能吗? 非常感谢:)

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

整理了 @SquidyDev 对其上述问题的评论的工作示例。

代码的顶部将 Squidy 的自定义函数“clear_selection”添加到plotly go.Figure 的定义中,然后可以在循环内调用该函数以清除先前的选择。

import numpy as np
from plotly import graph_objects as go


def clear_selection(self):
    """
    Custom function for cleaning selections from Plotly Figure

    """
    if 'selections' in self['layout']:
        self['layout']['selections'] = ()


go.Figure.clear_selection = clear_selection

# Normal plot, adding selection and looping through for user to interact with selection
t = np.linspace(0, 4*np.pi)
fig = go.Figure()
fig.add_scatter(
    x=t,
    y=np.sin(t),
)

for ii in range(5):
    fig.clear_selection()
    fig.add_selection(
        x0=ii, y0=-0.5,
        x1=ii+1, y1=0.75
    )
    fig.show()
    breakpoint()
© www.soinside.com 2019 - 2024. All rights reserved.