我想制作一个Jupyter笔记本,每次单击“绘图”按钮时都会绘制一些数据。问题是,每次单击时,都会在前一个图下创建新图。我希望能够清除现有的地块。我尝试添加一个名为plt.close('all')的“清除”按钮,但是它不起作用-原始图仍然存在。这里有一些示例代码来说明正在发生的事情:
#%%
%matplotlib notebook
from matplotlib import pyplot as plt
import numpy as np
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
from IPython.display import display
def eventhandler(obj)
nums=np.random.randint(low=1, high=1000, size=100)
plt.plot(nums)
def clearplots(obj):
plt.close('all')
button=widgets.Button(description='Plot', disabled=False)
button.on_click(eventhandler)
clear=widgets.Button(description='Clear', disabled=False)
clear.on_click(clearplots)
widgets.HBox([button,clear])
我在做什么错了?