【问题标题】:bqplot and ipywidget slider, maximum recursion depthbqplot 和 ipywidget 滑块,最大递归深度
【发布时间】:2018-06-28 20:34:11
【问题描述】:

如果我执行以下笔记本,一切似乎都正常。但是,当我移动滑块时,笔记本的响应能力几乎下降到零,一段时间后我收到以下错误:

RecursionError: maximum recursion depth exceeded in comparison

你能告诉我为什么吗?

# We'll start with bqplot's matplotlib inspired API

from bqplot import pyplot as plt

# Let's begin by importing some libraries we'll need
import numpy as np

import ipywidgets as wi

def make_data(x, sigma_noise):
    """
    Generates a sine wave

    :param x: x value, scalar or vector between 0 and ..
    :param sigma_noise: standard deviation of the noise added to the data
    """
    y = np.sin(x)
    noise = np.random.normal(loc=0, scale=sigma_noise, size=len(x))
    y_noise = y + noise   
    return y_noise


def update_plot(message):
    y_noise = make_data(x, slider.value)
    plot_1.y = y_noise

x = np.linspace(0, 10)
y = make_data(x, noise)

figure = plt.figure(title='Test', animation_duration=100)
#figure.animation_duration = 250
plot_1 = plt.scatter(x, y)
plot_1.observe(update_plot, ['x','y'])

slider = wi.FloatSlider(description='noise', value=0.001, min=0, max=1)
slider.observe(update_plot, 'value')

wi.VBox([slider, figure])

编辑:DougR 提供的解决方案有效。另外我找到了另一个解决方案:figure.observe(update_plot, ['x','y']) 而不是plot_1.observe(update_plot, ['x','y'])

【问题讨论】:

    标签: python widget jupyter bqplot


    【解决方案1】:

    您对 plot1 图进行了观察调用,该调用随后更新了 plot1 图...这会导致递归循环。如果您在函数中放置 if 语句,则可以对其进行设置,使其仅在第一次操作之后运行。

    # We'll start with bqplot's matplotlib inspired API
    
    from bqplot import pyplot as plt
    
    # Let's begin by importing some libraries we'll need
    import numpy as np
    
    import ipywidgets as wi
    
    def make_data(x, sigma_noise):
        """
        Generates a sine wave
    
        :param x: x value, scalar or vector between 0 and ..
        :param sigma_noise: standard deviation of the noise added to the data
        """
        y = np.sin(x)
        noise = np.random.normal(loc=0, scale=sigma_noise, size=len(x))
        y_noise = y + noise   
        return y_noise
    
    
    def update_plot(message):
    #     print(message)
        if message['name'] == 'value':
            y_noise = make_data(x, slider.value)
            plot_1.y = y_noise
    
    noise = 2
    x = np.linspace(0, 10)
    y = make_data(x, noise)
    
    figure = plt.figure(title='Test', animation_duration=100)
    #figure.animation_duration = 250
    plot_1 = plt.scatter(x, y)
    plot_1.observe(update_plot, ['x','y'])
    
    slider = wi.FloatSlider(description='noise', value=0.001, min=0, max=1)
    slider.observe(update_plot, 'value')
    
    wi.VBox([slider, figure])
    

    【讨论】:

    • 有趣。与此同时,我找到了另一个解决方案:figure.observe 而不是 plot_1.observe。我不知道它为什么会起作用,但不知何故,更新自身的情节会导致这个错误似乎是有道理的。
    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多