Reputation: 3879
I have an ipywidget that generates a plot each time the selection changes. Currently the plots are appended to each other; however, I would like to have only one plot (the latest).
import ipywidgets as widgets
import matplotlib.pyplot as plt
def f(change):
if change['type'] == 'change' and change['name'] == 'value':
plt.close('all') # seems to have no effect
plt.plot([0, 1],[0, 10])
plt.title(change['new'])
plt.show()
w = widgets.Select(options=[0, 1])
w.observe(f)
display(w)
I tried without success:
Note: I'm not using interactive
since I do not want to run all functions when any parameter changes.
Upvotes: 1
Views: 1707
Reputation: 5565
Try sending your charts to an Output
widget, which can be used as a context manager, and then cleared with out.clear_output()
.
import ipywidgets as widgets
import matplotlib.pyplot as plt
%matplotlib inline
out = widgets.Output(layout = widgets.Layout(height='300px'))
def f(change):
if change['type'] == 'change' and change['name'] == 'value':
with out:
plt.plot([0, 1],[0, 10])
plt.title(change['new'])
out.clear_output()
plt.show()
w = widgets.Select(options=[0, 1])
w.observe(f)
display(w)
display(out)
Upvotes: 2