ipywidgets + matplotlib无法清除旧的输出 [英] ipywidgets + matplotlib cannot clear old outputs

查看:111
本文介绍了ipywidgets + matplotlib无法清除旧的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from ipywidgets import interact, FloatSlider, RadioButtons

amplitude_slider = FloatSlider(min=0.1, max=1.0, step=0.1, value=0.2)
color_buttons = RadioButtons(options=['blue', 'green', 'red'])
# decorate the plot function with an environment from the UIs:
@interact(amplitude=amplitude_slider, color=color_buttons)
def plot(amplitude, color):
    fig, ax = plt.subplots(figsize=(4, 3),
                       subplot_kw={'axisbg':'#EEEEEE',
                                   'axisbelow':True})

    ax.grid(color='w', linewidth=2, linestyle='solid')
    x = np.linspace(0, 10, 1000)
    ax.plot(x, amplitude * np.sin(x), color=color,
        lw=5, alpha=0.4)
    ax.set_xlim(0, 10)
    ax.set_ylim(-1.1, 1.1)

多次使用小部件为我带来了多个输出:

Using the widget multiple times got me multiple outputs:

我需要在代码中更新哪些内容,以便每次使用小部件时都能清除绘图?

What do I need to update in the code so the plot is cleared each time the widget is used?

推荐答案

我认为可能存在两个问题:

I think there might be two problems:

  1. 首先是,每次更新交互式元素时,您都将创建一个新的图形和轴.因此,将添加这个新数字.
  2. 第二,您正在使用%matplotlib inline,它显示了绘图的图像.因此,更改绘图后,将显示新图像,而不是当前图像.
  1. The first is that you create a new figure and axes each time the interactive elements are updated. So this new figure will be appended.
  2. Second, you are using the %matplotlib inline, which shows images of the plot. So once the plot is changed, a new image will be displayed instead of the current one changed.

一个选项可能是使用%matplotlib notebook后端.

An option might be to use the %matplotlib notebook backend.

然后将创建一次图形并绘制一些初始值.我们还需要保留对艺术家的引用,该引用应由小部件进行更新.

One would then create the figure once and plot some initial values. We would also need to keep a reference to the artist, which should be updated by the widgets.

完成后,交互式图形似乎与交互式小部件冲突.为了解决这个问题,我将所有交互式小部件(包括它们的导入)放在该图下方的新单元格中.

Once that is done, the interactive figure seems to conflict with the interactive widgets. In order to resolve this, I put all the interactive widgets (including their imports) in a new cell below the figure.

我不确定发生这种情况的原因,但是以下似乎是可行的解决方案.

进口

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

创建图形,绘制初始线

fig, ax = plt.subplots(figsize=(4, 3),
                       subplot_kw={'facecolor':'#EEEEEE',
                                   'axisbelow':True})
ax.grid(color='w', linewidth=2, linestyle='solid')
x = np.linspace(0, 10, 1000)
line, = ax.plot(x, 0.2 * np.sin(x), color="blue",
    lw=5, alpha=0.4)
ax.set_xlim(0, 10)
ax.set_ylim(-1.1, 1.1) 

最后,在一个新单元中进行交互,仅更新艺术家,而不重新创建他们.

Finally, in a new cell, do the interaction, only update the artists, not recreate them.

from ipywidgets import interact, FloatSlider, RadioButtons
amplitude_slider = FloatSlider(min=0.1, max=1.0, step=0.1, value=0.2)
color_buttons = RadioButtons(options=['blue', 'green', 'red'])
# decorate the plot function with an environment from the UIs:
@interact(amplitude=amplitude_slider, color=color_buttons)
def plot(amplitude, color):
    y = amplitude * np.sin(x)
    line.set_ydata(y)
    line.set_color(color)

这是它的外观图像.

这篇关于ipywidgets + matplotlib无法清除旧的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆