Ipywidgets + matplotlib cannot clear old results

%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, I got multiple outputs:

enter image description here

What do I need to update in the code so that the graph is cleared every time the widget is used?

+3


source to share


1 answer


I think there could be two problems:

  • First, you create a new shape and axes every time interactive elements are updated. Thus, this new shape will be added.
  • Second, you are using %matplotlib inline

    which shows the plot images. Therefore, as soon as the plot is changed, the new image will change instead of the current one.

An option can be to use a backend %matplotlib notebook

.

Then one could create the drawing once and display some initial values. We will also need to leave a link to the artist to be updated with the widgets.

Once this is done, the interactive shape appears to be in conflict with the interactive widgets. To solve this problem, I placed all interactive widgets (including their imports) in a new cell below the number.

I'm not sure about the reasons for this, but the following seems to be a working solution.

Import



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

      

Create a drawing, draw a starting line

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 the new cell, do the interaction, update only the artists, don't create 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)

      

Here is a picture of what it will look like.

enter image description here

+2


source







All Articles