我正在jupyter笔记本中运行以下代码。它基于SelectMultiple小部件中的选择生成一些图。当我直接使用plot方法inst.plot()
时,输出有效,仅显示一个图形。但是,当我使用该按钮创建新图时,不会清除输出,而不是仅显示一个图,而是显示多个图形。任何想法为什么会发生以及如何解决? GitHub Issue
%pylab inline
import pandas as pd
import numpy as np
import ipywidgets as widgets
from ipywidgets import SelectMultiple, Select, Layout, Button, VBox
from ipywidgets import IntProgress as Progress
from IPython.display import display, clear_output
class TheClass():
def __init__(self):
self._xmax = 0
self._xmin = 10
self.selector_steps = SelectMultiple(options=[5, 10, 100, 1000], value=(10, 100))
self.output = widgets.Output()
self.n_steps = 10
self.button_plot = Button(description='Plot')
self.button_plot.on_click(self.plot)
@property
def xmax(self):
return self._xmax
@xmax.setter
def xmax(self, value):
self._xmax = value
def x(self):
return np.linspace(self._xmin, self._xmax, self.n_steps)
def y(self):
return np.sin(self.x())
def plot(self, button=None):
with self.output:
clear_output()
self._create_plots()
show()
display(self.output)
#@lru_cache(maxsize=32)
def _create_plots(self):
plots = []
figure()
for n_steps in self.selector_steps.value:
self.n_steps = n_steps
plt.plot(self.x(), self.y())
inst = TheClass()
VBox([inst.selector_steps, inst.button_plot])