【问题标题】:How to prevent recursion with interactive plot in bqplot?如何防止在 bqplot 中使用交互式绘图进行递归?
【发布时间】:2020-11-28 17:25:23
【问题描述】:

我使用bqplot 创建了一个交互式散点图,您可以在其中拖动点(使用enable_move=True)。

我不希望用户将点拖到线 y=x 上方。 如果他们这样做了,我希望该点恢复到最近的位置。

问题是我不确定如何在这里避免无限递归。

散点图需要知道它的点何时移动,以便检查移动并可能回弹。 然而,当它开始回弹时,这种(点位置的)变化似乎触发了同样的回调。

谁能告诉我处理这个基本问题的“正确”方法?

import bqplot.pyplot as plt
import numpy as np

def on_point_move(change, scat):
    if np.any(newx < scat.y):
        scat.x = change['old']
    
fig = plt.figure(animation_duration=400)
xs = 1.0*np.arange(3) # make sure these are floats
ys = 1.0*np.arange(3)
scat = plt.scatter(xs, ys, colors=['Red'], default_size=400, enable_move=True)
scat.observe(lambda change: on_point_move(change, scat), names=['x'])
fig

【问题讨论】:

    标签: recursion plot interactive bqplot


    【解决方案1】:

    您可以在 on_point_move 函数中暂时禁用观察。我也稍微改了一下逻辑。

    import bqplot.pyplot as plt
    import numpy as np
    
    def on_point_move(change):
        if np.any(scat.x < scat.y):
            scat.unobserve_all()
            if change['name'] == 'x':
                scat.x = change['old']
            elif change['name'] == 'y':
                scat.y = change['old']
            scat.observe(on_point_move, names=['x','y'])
        
    
    fig = plt.figure(animation_duration=400)
    xs = 1.0*np.arange(3) # make sure these are floats
    ys = 1.0*np.arange(3)
    scat = plt.scatter(xs, ys, colors=['Red'], default_size=400, enable_move=True)
    scat.observe(on_point_move, names=['x','y'])
    
    fig
    

    【讨论】:

    • 谢谢@DougR!我一直在寻找一种过滤或处理交互的方法。完全删除它当然可以解决问题。
    猜你喜欢
    • 2012-05-12
    • 2014-04-05
    • 2012-05-29
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多