我正在研究一些计算机视觉算法,我想展示一个numpy数组在每一步中如何变化.
现在有用的是,如果imshow( array )我的代码末尾有一个简单的,窗口会显示并显示最终的图像.
然而,我想要做的是更新并显示imshow窗口,因为图像在每次迭代中都会发生变化.
例如,我想做:
import numpy as np
import matplotlib.pyplot as plt
import time
array = np.zeros( (100, 100), np.uint8 )
for i in xrange( 0, 100 ):
for j in xrange( 0, 50 ):
array[j, i] = 1
#_show_updated_window_briefly_
plt.imshow( array )
time.sleep(0.1)
Run Code Online (Sandbox Code Playgroud)
问题是这样,只有整个计算完成后,Matplotlib窗口才会被激活.
我已经尝试了原生matplotlib和pyplot,但结果是一样的.为了绘制命令我找到了一个.ion()开关,但在这里它似乎不起作用.
Q1.连续显示numpy数组更新的最佳方法是什么(实际上是uint8灰度图像)?
Q2.是否可以使用动画功能执行此操作,如动态图像示例中所示?我想在循环中调用一个函数,因此我不知道如何使用动画函数来实现它.
我找到了这个关于动画的精彩短篇教程:
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
但是我不能制作一个同样时尚的动画imshow()情节.我试图替换一些行:
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 10), ylim=(0, 10))
#line, = ax.plot([], [], lw=2)
a=np.random.random((5,5))
im=plt.imshow(a,interpolation='none')
# initialization function: plot the background of each frame
def init():
im.set_data(np.random.random((5,5)))
return im
# animation function. This is called sequentially
def animate(i):
a=im.get_array()
a=a*np.exp(-0.001*i) # exponential decay of the values
im.set_array(a)
return im
Run Code Online (Sandbox Code Playgroud)
但我遇到了错误,你可以帮助我运行吗?先感谢您.最好,
我试图用来matplotlib.ArtistAnimation动画两个子图.我希望随着动画的进行,x轴的值增加,这样动画的总长度就是100,但是在任何时候,子图只向我显示0-24的时间值,然后迭代到100.
这里给出了一个很好的例子.该链接使用FuncAnimation并plot().axes.set_xlim()递增x值以滚动方式使用和更新x轴标签.该代码可通过所提供链接中YouTube视频下方的链接获取.
我在下面附加了代码,显示了我尝试复制这些结果但x限制似乎采用了它们的最终值而不是随时间递增.我还尝试通过仅绘制将在子图中看到的窗口中的值来递增解(而不是轴),但不会增加x轴值.我也试图实现自动缩放,但x轴仍然没有更新.
我也发现这个问题实际上是同一个问题,但问题从来没有得到解答.
这是我的代码:
import matplotlib.pylab as plt
import matplotlib.animation as anim
import numpy as np
#create image with format (time,x,y)
image = np.random.rand(100,10,10)
#setup figure
fig = plt.figure()
ax1=fig.add_subplot(1,2,1)
ax2=fig.add_subplot(1,2,2)
#set up viewing window (in this case the 25 most recent values)
repeat_length = (np.shape(image)[0]+1)/4
ax2.set_xlim([0,repeat_length])
#ax2.autoscale_view()
ax2.set_ylim([np.amin(image[:,5,5]),np.amax(image[:,5,5])])
#set up list of images for animation
ims=[]
for time in xrange(np.shape(image)[0]):
im = ax1.imshow(image[time,:,:])
im2, = ax2.plot(image[0:time,5,5],color=(0,0,1))
if …Run Code Online (Sandbox Code Playgroud)