Jupy中matplotlib plot的连续更新

2024-05-11 20:06:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个Jupyter Notebook,我正在使用以下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 array进行掩蔽:

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 cell中,因此当我更改Thr的值时,我必须再次手动运行发生掩蔽和绘图的单元格。你知道吗

我的问题是:我经常看到那些交互式绘图,在这里您可以更改参数(在我的例子中是ipywidgetThr),然后自动更新绘图。你知道吗

我看到widgets.IntSlider有一个continuous_update参数,它似乎接近我想要的,但仍然无法获得我想要的行为。你知道吗

你知道这是否可行吗?你知道吗

编辑

从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个图像,所以我想让他们显示如下,每一个与它旁边的滑块来完成任务:(下面的图像不是真实的,只是弥补了代表我想要的)

enter image description here

编辑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.write(X)中的numpy数组


Tags: importnumpyvalueasdisplayupdatepltwidgets
1条回答
网友
1楼 · 发布于 2024-05-11 20:06:22

我已经将我给出的示例改编为一个类,因为您希望链接一个特定的输出和slider实例,但要创建多个组。设置输出小部件的布局可以避免滑动滑块时小部件一直在调整大小。你知道吗

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

# setup figure
n = 10

class SliderAndImage():

    # show random mesh
    def update(self, idx):
        with self.out:
            clear_output()
            fig, ax = plt.subplots(figsize = (5,5))
            h = ax.imshow(np.random.rand(n, n))
            h.set_data(np.random.rand(n, n))
            fig.canvas.flush_events()
            fig.canvas.draw()
            plt.show()

    def make_slider_and_image(self):

        self.out = ipy.Output(layout=ipy.Layout(width='200px', height='200px'))

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

        layout = ipy.Layout(
        #     display = 'flex',
        #                    flex_flow = 'row',
        #                    justify_content = 'space-between',
        #                    align_items = 'center',
                           )
        widgets = ipy.HBox(children=(slider, self.out), layout = layout)
        return widgets

children = []
for _ in range(3):
    widgets = SliderAndImage()
    children.append(widgets.make_slider_and_image())
display(ipy.HBox(children))

enter image description here

相关问题 更多 >