首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bqplot和ipywidget滑块,最大递归深度

bqplot和ipywidget滑块,最大递归深度
EN

Stack Overflow用户
提问于 2018-06-29 04:34:11
回答 1查看 340关注 0票数 0

如果我执行下面的Notebook,一切似乎都很好。然而,当我移动滑块时,笔记本的响应性几乎降到了零,一段时间后,我得到了以下错误:

代码语言:javascript
复制
RecursionError: maximum recursion depth exceeded in comparison

你能告诉我为什么吗?

代码语言:javascript
复制
# 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'])

EN

回答 1

Stack Overflow用户

发布于 2018-07-09 23:46:49

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

代码语言:javascript
复制
# 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])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51090853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档