Python Jupyter中matplotlib绘图的连续更新

Python Jupyter中matplotlib绘图的连续更新,python,matplotlib,jupyter-notebook,ipywidgets,Python,Matplotlib,Jupyter Notebook,Ipywidgets,我正在使用Jupyter笔记本电脑,并使用以下ipywidget设置阈值: Thr = widgets.IntSlider(value=-17, min=-30, max=-13, step=1, description='Threshold: ', disabled=False, continuous_update=True, orientation='horizontal', readout=True, readout_format='d') Thr 接下来,我将使用该值屏蔽一个numpy

我正在使用Jupyter笔记本电脑,并使用以下ipywidget设置阈值:

Thr = widgets.IntSlider(value=-17, min=-30, max=-13, step=1, description='Threshold: ', disabled=False, continuous_update=True, orientation='horizontal', readout=True, readout_format='d')
Thr
接下来,我将使用该值屏蔽一个numpy数组,包括:

import numpy.ma as ma
test= ma.masked_less_equal(S_images[0], Thr.value)
最后,我将结果绘制为:

plt.figure(figsize = (15,15))
plt.imshow(test[0], cmap='gray')
ipywidget位于与其他代码不同的Jupyter单元格中,因此当我更改Thr的值时,我必须再次手动运行进行掩蔽和绘图的单元格

我的问题是:我总是看到那些交互式绘图,在我的例子中,你可以更改一个参数ipywidget Thr,然后自动更新绘图

我看到widgets.IntSlider有一个持续更新参数,它似乎接近我想要的,但仍然无法获得我想要的行为

你知道这是可行的还是可能的吗

_编辑

从ac24的评论开始,我采用了他提出的例子:

from IPython.display import display, clear_output
import ipywidgets as ipy
import matplotlib.pyplot as plt
import numpy as np

# setup figure
n = 10

out = ipy.Output()

# show random mesh
def update(idx):
    with out:
        clear_output()
        fig, ax = plt.subplots(figsize = (5,5))
        h = ax.imshow(S_images[0]) # here I put my image
        h.set_data(np.ma.masked_less_equal(S_images[0], slider.value)) # here I set the task to masked accordint to the `slider.value`
        fig.canvas.flush_events()
        fig.canvas.draw()
        plt.show()

slider = ipy.IntSlider(min = 0, max = 10, orientation = 'vertical')
widget = ipy.interactive(update, idx = slider)

layout = ipy.Layout(
#     display = 'flex',
#                    flex_flow = 'row',
#                    justify_content = 'space-between',
#                    align_items = 'center',
                   )
widgets = ipy.HBox(children=(slider, out), layout = layout)
display(widgets)
这个例子非常好用,正是我想要的。然而,我有一个关于布局的小问题。最初,我正在处理3个图像,所以我希望它们显示如下,每个图像旁边都有一个滑块来执行任务:下面的图像不是真实的,只是为了表示我想要的

编辑2

在这种情况下,问题是,一旦我在滑块中选择了一个值,我会将该光栅写入geotiff。为此,我使用以下代码:

with rasterio.open('/Path/20190331_VV_Crop') as src:
    ras_meta = src.profile

with rasterio.open('/path/Threshold.tif', 'w', **ras_meta) as dst:
    dst.write(X)

但是,我不知道如何引用dst.writeX中的numpy数组,我已经将我给出的示例改编为一个类,因为您希望链接特定的输出和滑块实例,但创建多个组。设置输出小部件的布局可以避免滑动滑块时小部件一直在调整大小

从IPython.display导入显示中,清除\u输出 将ipywidgets作为ipy导入 将matplotlib.pyplot作为plt导入 将numpy作为np导入 安装图 n=10 类滑块尺寸: 显示随机网格 def updateself,idx: 使用self.out: 清除输出 图,ax=plt.SubPlotsIgSize=5,5 h=ax.imshownp.random.randn,n h、 set_datanp.random.randn,n 图canvas.flush\u事件 无花果画 节目 def make_slider_和_imageself: self.out=ipy.Outputlayout=ipy.Layoutwidth='200px',height='200px' slider=ipy.IntSlidermin=0,最大值=10,方向为“垂直” widget=ipy.interactiveself.update,idx=slider 布局=ipy.layout 显示='flex', flex_flow='行', justify_content='space between', 对齐_项='居中', widgets=ipy.HBoxchildren=slider,self.out,layout=layout 返回小部件 儿童=[] 对于范围3中的uu: widgets=SliderAndImage children.appendwidgets.make_滑块和图像 displayipy.HBoxchildren
另一个问题中有一个简单的例子。它应该提供交互功能的基础知识以及如何显示图表。“continuous_update”参数提供了一些不同的功能,请在我的示例中试用它,看看您是否能够了解它的工作原理。@ac24非常感谢您的评论。如果你加上它作为一个答案,我将很高兴接受它。我只是对布局有一点意见。我已编辑我的问题以添加问题。你能评论一下吗?交互式绘图的好例子。许多人编辑了这个问题,添加了一个小的相关问题