缓慢的matplotlib和ipywidgets图像刷新 [英] Slow matplotlib and ipywidgets image refresh

查看:148
本文介绍了缓慢的matplotlib和ipywidgets图像刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用plt.show()和ipywidgets interact()函数在一些磁共振切片中滚动.缓慢移动滑块没有发现任何问题,但是在切片中滑动得更快时,发现了一个重要的延迟.

I'm trying to scroll through some magnetic resonance slices using plt.show() and ipywidgets interact() function. I've found no problem by slowly moving the slider, but an important delay is found when sliding through the slices a little bit faster.

以下是我正在使用的代码.

Here below is the code I'm using.

def dicom_animation(x, volume):
    fig = plt.figure(figsize=(8,8))
    plt.imshow(volume['slices'][x]['pixel_array'], cmap=plt.cm.gray)


interact(dicom_animation, volume = fixed(a), x=IntSlider(round(len(a['slices'])/2,0), 0, (len(a['slices'])-1), layout=Layout(width='500px')))

然后就是结果.

在没有这个重要延迟的情况下,我是否可以做些什么来使滑动更快一点?

Is there anything I can do to make the sliding a little bit faster without this important delay?

推荐答案

重塑图形并调用plt.imshow都是非常昂贵的操作,这使速度变慢.相反,您需要使用交互式matplotlib后端,然后使用类似set_data的方法.

Remaking the figure and calling plt.imshow are both pretty expensive operations which is what is slowing this down. Instead you need to use an interactive matplotlib backend and then use methods like set_data.

ipympl 是matplotlbi的交互式后端,可在jupyter nobteook和jupyterlab中使用.它有一个很好的示例笔记本,在这里解释了诸如如何与其他小部件交互的内容:

ipympl is an interactive backend for matplotlbi that works in both jupyter nobteook and jupyterlab. It has a nice example notebook that explains things like how to interact with other widgets here: https://github.com/matplotlib/ipympl/blob/0.6.1/examples/ipympl.ipynb

对于JupyterLab 3+,安装此程序要容易得多,所以:

Installing this is significantly easier for JupyterLab 3+ So:

pip install --upgrade jupyterlab ipympl

手动创建滑块

虽然interact很方便,但是它并不希望与ipympl一起很好地发挥作用,因为它希望每次滑块改变时都能完全重新生成输出.

manually create a slider

While interact is convenient it doesn't tend to play well with ipympl as it expects to completely regenerate the output everytime the slider changes.

%matplotlib ipympl

# the rest of your setup code

fig, ax = plt.figure()
img = ax.imshow(volume['slices'][x]['pixel_array'], cmap=plt.cm.gray)

def update_plot(change):
    img.set_data(volume['slices'][change['new']['pixel_array']
    fig.canvas.draw_idle()

x_slider = IntSlider(round(len(a['slices'])/2,0), 0, (len(a['slices'])-1)
x_slider.observe(update_plot, names='value')

使用mpl-interactions

手动设置滑块可能真是麻烦.因此,我编写了一个库,使使用小部件轻松控制交互式绘图变得容易.它处理创建滑块并完成所有正确的操作,例如为您使用set_data.如果您不在Jupyter笔记本电脑中,它还将使用matplotlib滑块,因此更便于携带.

Use mpl-interactions

Manually setting up the slider can be a real hassle. So I wrote a library that to make it easy to control interactive plots using widgets. It handles creating the sliders and does all the correct things such as using set_data for you. It also will use matplotlib sliders if you aren't in a jupyter notebook so it is more portable.

对于您而言,您可能会对 imshow示例 或根据数据的结构,还可以使用超切片机.

In your case you would be interested in the imshow example or depending on how your data is structured you may also be able to use the hyperslicer.

您的示例将是:

%matplotlib ipympl

import mpl_interactions.ipyplot as iplt
# other setup stuff
volume = a

def f(x):
    return volume['slices'][x]['pixel_array']

fig, ax = plt.subplots()
controls = iplt.imshow(f, cmap=plt.cm.gray, x = np.arange(0, len(a)-1))

这篇关于缓慢的matplotlib和ipywidgets图像刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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