如何重用python plot.ly update\u点回调?

2024-05-07 13:05:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下函数用于更新python plot.ly中的一个点:

def arbitrary_plot_fn():

    # define data
    ...

    # define figure widget
    fig=...

    # define update_point fn
    def update_point(trace, points, selector):
        c = list(scatter.marker.color)
        s = list(scatter.marker.size)
        xs = np.array(points.xs)
        ys = np.array(points.ys)

        for i in points.point_inds:
            print(i,results[i,0])
            c[i] = available_colors[2]
            s[i] = 20
            with fig.batch_update():
                scatter.marker.color = c
                scatter.marker.size = s

    # works
    scatter.on_click(update_point)

    # would prefer the following (which doesn't work) ~:
    scatter.on_click(update_point(fig=fig)) # obviously would add a kwarg to update_point()

    fig.show()

    return

由于update_point隐式地需要fig,因此必须为每个图形重新定义该函数,这会导致大量冗余代码

我可以在每次重新定义时使用lambda来缩短回收时间,每个fig一个

有没有一种方法可以通过简单地将fig作为参数传递给update_point来实现这一切