gpt4 book ai didi

python - Jupyter:如何在单击按钮时更新绘图(ipywidgets)

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:14 25 4
gpt4 key购买 nike

我正在使用 Jupyter 并尝试让我的绘图具有交互性。

所以我有一个情节。我有一个 ipywidgets 按钮。

单击按钮时我需要更新绘图,就像与 slider 交互一样。

但我不能。

它仅在 matplotlib 使用“notebook”后端时有效,但它看起来很糟糕。同时与任何类型的情节进行互动。有什么方法可以重现这个,而不是使用交互?

#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()

最佳答案

作为解决方法,我们可以将整个绘图重新绘制到输出小部件,然后在不闪烁的情况下显示它。

%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

关于python - Jupyter:如何在单击按钮时更新绘图(ipywidgets),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com