Ant*_*kov 4 python matplotlib jupyter ipywidgets
我正在使用 Jupyter 并尝试使我的绘图具有交互性。
所以我有一个阴谋。我有一个 ipywidgets 按钮。
单击按钮时,我需要更新绘图,就像与滑块进行交互一样。
但我不能。
它仅在 matplotlib 使用“笔记本”后端时才有效,但看起来很糟糕。同时与任何类型的情节进行交互。有没有办法重现这一点,而不是使用交互?
#this works fine! But toolbar near the graph is terible
#%matplotlib notebook
#this does not work
%matplotlib inline
from matplotlib.pyplot import *
button = ipywidgets.Button(description="Button")
def on_button_clicked(b):
ax.plot([1,2],[2,1])
button.on_click(on_button_clicked)
display(button)
ax = gca()
ax.plot([1,2],[1,2])
show()
Run Code Online (Sandbox Code Playgroud)
作为解决方法,我们可以将整个图重新绘制到输出小部件,然后显示它而不会闪烁。
%matplotlib inline
from matplotlib.pyplot import *
button = ipywidgets.Button(description="Button")
out = ipywidgets.Output()
def on_button_clicked(b):
with out:
clear_output(True)
plot([1,2],[2,1])
show()
button.on_click(on_button_clicked)
display(button)
with out:
plot([1,2],[1,2])
show()
out
Run Code Online (Sandbox Code Playgroud)