Jupyter:如何在单击按钮时更新绘图(Ipywidgets) [英] Jupyter: How to update plot on button click (ipywidgets)

查看:56
本文介绍了Jupyter:如何在单击按钮时更新绘图(Ipywidgets)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以我有一个图。我有一个ipywidgets按钮。

单击按钮时,我需要更新绘图,就像使用滑块进行交互一样。

但我不能。

只有当matplotlib使用‘Notebook’后端时,它才能工作,但它看起来很糟糕。同时,Interactive可以处理任何类型的情节。是否有方法可以不使用InterAct重现此内容?

#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

这篇关于Jupyter:如何在单击按钮时更新绘图(Ipywidgets)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆