我正在尝试用chaco制作一组2D图像的动画,但不幸的是,它似乎没有我的应用程序需要的那么快。目前,我正在构建一个chaco Plot并使用img_plot,例如:
pd = ArrayPlotData()
pd.set_data("imagedata", myarray)
plot = Plot(pd)
plot.img_plot("imagedata", interpolation="nearest")为了更新图像,我使用以下命令:
pd.set_data("imagedata", my_new_array)这是可行的,但是速度还不够快。有没有办法加快速度呢?是否有更低级别的函数可以更快地更新图像?
发布于 2015-09-26 01:13:08
这只是一个想法,但最初将每个图像添加到ArrayPlotData中就能解决您的问题吗?这样,您就不必在动画中的每一步都添加新图像,只需在下一系列中调用img_plot()即可。例如,如果您的图像存储在一个名为imagesnt,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)这会自动将每个镜像命名为“Series1”、“Series2”等,然后您可以调用:
plot.img_plot('series1', interpolation = 'nearest') #or 'series2' etc. 动画中的每个图像,而不必调用set_data()。
您可以获得图像名称的排序列表'series1,'series2',...要使用以下内容进行迭代:
from natsort import natsorted #sort using natural sorting
names = natsorted(pd.list_data())这对解决瓶颈有帮助吗?
发布于 2015-09-29 09:55:34
这是一个我如何使用计时器在Chaco中制作动画的例子。通常,诀窍(正如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()我得到的东西是这样的:

https://stackoverflow.com/questions/32771150
复制相似问题