简体   繁体   English

如何加快查科图像的绘制

[英]How to speed up chaco image plot

I am trying to animate a bunch of 2D images with chaco, but unfortunately it does not seem to be as fast as my application needs it. 我正在尝试使用chaco为一堆2D图像制作动画,但是不幸的是,它似乎并没有我的应用程序所需的速度快。 At the moment I am building a chaco Plot and using img_plot , eg: 目前,我正在构建一个img_plot Plot并使用img_plot ,例如:

pd = ArrayPlotData()
pd.set_data("imagedata", myarray)
plot = Plot(pd)
plot.img_plot("imagedata", interpolation="nearest")

And to update the image, I use the following: 为了更新图像,我使用以下命令:

pd.set_data("imagedata", my_new_array)

This works, however is not fast enough. 这行得通,但是还不够快。 Is there any way to speed it up? 有什么办法可以加快速度吗? Any lower-level function that allows a faster update of the image? 是否有任何较低级别的功能可以更快地更新图像?

This is just a thought, but would adding every image initially into your ArrayPlotData solve your problem? 这只是一个想法,但是最初将每个图像添加到ArrayPlotData中是否可以解决您的问题? Then you aren't adding a new image at each step in your animation, and just calling img_plot() on the next series. 然后,您无需在动画的每个步骤中添加新图像,而只需在下一个系列中调用img_plot()。 For example, if your images are stored in a numpy array called images[nt, nx, ny]: 例如,如果您的图像存储在名为images [nt,nx,ny]的numpy数组中:

pd = ArrayPlotData()
for index in range(images.shape[0]): #Assuming you want to iterate over nt
    pd.set_data('', images[index,:,:], generate_name = True)
plot = Plot(pd)

This automatically names each image 'series1', 'series2', etc. Then you can call: 这会自动将每个图像命名为“ series1”,“ series2”等。然后您可以调用:

plot.img_plot('series1', interpolation = 'nearest') #or 'series2' etc. 

for every image in your animation without having to call set_data(). 动画中的每个图像,而无需调用set_data()。

You can get a sorted list of your image names ['series1, 'series2', ...] to iterate over using: 您可以使用以下顺序获得图像名称的排序列表['series1,'series2',...]进行迭代:

from natsort import natsorted #sort using natural sorting
names = natsorted(pd.list_data())

Would that help with the bottleneck? 这对瓶颈有帮助吗?

Here's an example of how I do animations in Chaco using a timer. 这是一个如何使用计时器在Chaco中制作动画的示例。 Usually the trick (as J Corson said) is to load your data into an array and then just use an index to take successive slices of the array. 通常的窍门(如J Corson所说)是将数据加载到数组中,然后仅使用索引来获取数组的连续切片。

from chaco.api import ArrayPlotData, Plot
from enable.api import ComponentEditor
import numpy as np
from pyface.timer.api import Timer
from traits.api import Array, Bool, Event, HasTraits, Instance, Int
from traitsui.api import ButtonEditor, Item, View


class AnimationDemo(HasTraits):
    plot = Instance(Plot)
    x = Array
    y = Array
    run = Bool(False)
    go = Event
    idx = Int

    def _x_default(self):
        x = np.linspace(-np.pi, np.pi, 100)
        return x

    def _y_default(self):
        phi = np.linspace(0, 2 * np.pi, 360)
        y = np.sin(self.x[:, np.newaxis] + phi[np.newaxis, :]) - \
            0.1 * np.sin(13 * self.x[:, np.newaxis] - 7 * phi[np.newaxis, :])
        return y

    def _plot_default(self):
        plot_data = ArrayPlotData(y=self.y[:, 0], x=self.x)
        plot = Plot(plot_data)
        plot.plot(('x', 'y'))
        return plot

    def _go_fired(self):
        if not self.run:
            self.run = True
        else:
            self.run = False

    def _run_changed(self):
        if self.run:
            self.timer.Start()
        else:
            self.timer.Stop()

    def _run_default(self):
        self.timer = Timer(5, self._timer_tick)
        return False

    def _timer_tick(self):
        if not self.run:
            raise StopIteration
        else:
            if self.idx >= 360:
                self.idx = 0
            self.plot.data.set_data('y', self.y[:, self.idx])
            self.idx += 1

    traits_view = View(
        Item('plot', editor=ComponentEditor(), show_label=False),
        Item('go', editor=ButtonEditor(label="Start/Stop"), show_label=False),
    )


if __name__ == "__main__":
    ad = AnimationDemo()
    ad.edit_traits()

I get something like this: 我得到这样的东西:

Chaco演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM