与 ipywidgets 事件交互和绘图会产生许多图表

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

我正在尝试使用小部件事件来制作交互式图表。

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

import ipywidgets as widgets

def myplot(n):
    x = np.linspace(-5, 5, 30)
    y = x**n

    fig, ax = plt.subplots(nrows=1, ncols=1);
    ax.plot(x, y)
    ax.set_xlabel('x')
    ax.set_ylabel('y')

    plt.show()

Interact 按预期工作(它以交互方式更改图形):

widgets.interact(myplot, n=(0,5));

但是,以下代码片段会创建几个在您与滑块交互时出现在下面的图形。

n_widget = widgets.IntSlider(
                value=2,
                min=0,
                max=5)

def on_value_change(change):
    myplot(n=n_widget.value)

n_widget.observe(on_value_change)
display(n_widget)

我可以像使用 widgets.interact() 一样更新绘图吗?


我当前的安装是使用 conda 和 Python 3.6(Windows 机器)。

ipywidgets                7.1.0                     
jupyter                   1.0.0              
jupyter_client            5.2.1                  
jupyter_console           5.2.0             
jupyter_core              4.4.0              
matplotlib                2.1.1             
notebook                  5.3.1               
numpy                     1.14.0     
python-3.x matplotlib widget jupyter-notebook interactive
2个回答
5
投票

请注意,以下是 ipywidgets 版本 < 7.0. For a solution with ipywidgets >= 7.0 的工作解决方案,请参阅 此 GitHub 问题

虽然在许多简单情况下

plt.show()
可以很好地替换单元格的输出,但情况并非总是如此。在 Jupyter 中使用交互元素时,使用
IPython.display.display
通常更有帮助。

在这里,您可能不想为每次交互创建新的绘图。相反,只需为绘图设置新数据就足够了。然后,您可以自动缩放新数据的绘图并显示图形。一旦显示新图形,您可以使用

IPython.display.clear_output
清除输出。这确保了输出单元格中始终存在单个图,而与使用
interact
observe
无关。

def myplot(n):
    line.set_ydata(x**n)
    ax.relim()
    ax.autoscale()
    display(fig)
    clear_output(wait=True)

完整笔记本:

# cell 1
%%capture
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display, clear_output
import ipywidgets as widgets

fig, ax = plt.subplots(nrows=1, ncols=1);
x = np.linspace(-5, 5, 30)
y = x**0

line, = ax.plot(x, y)
ax.set_xlabel('x')
ax.set_ylabel('y')

def myplot(n):
    line.set_ydata(x**n)
    ax.relim()
    ax.autoscale()
    display(fig)
    clear_output(wait=True)

#cell2
widgets.interact(myplot, n=(0,5));

#cell3
n_widget = widgets.IntSlider(
                value=2,
                min=0,
                max=5)

def on_value_change(change):
    myplot(n=n_widget.value)

n_widget.observe(on_value_change)
display(n_widget)


0
投票

更容易做到:

display(fig)

plt.close(fig)

所以你不必clear_output(清除所有内容)。

© www.soinside.com 2019 - 2024. All rights reserved.