2014-04-14 109 views
9

我正在尝试对某些时间相关数据的空间坐标的wigner函数进行动画处理。 wigner函数是二维的,所以我使用contourf()来绘制它。我将数据存储在一个HDF5文件中,并可以快速制作Wigner发行版,但我无法弄清楚如何为它制作动画。我所能找到的所有动画教程和示例(例如this onethis one)都严格用于线条图。具体而言,他们的animate(i)功能使用line.set_data(),我似乎无法找到contourf()的等效项。如何使用contourf()制作动画?

我如何用动画制作contourf()图像?

什么是contourf()相当于set_data()

+0

[使用matplotlib.animate动画在python的等值线图]的可能重复(http://stackoverflow.com/questions/16915966/using-matplotlib-animate-to-animate-a-contour-plot -in-python) – Dan

+0

我所做的是每次创建一个新的等高线图,并隐藏旧的'cont.set_alpha(0)'。哈克。应该有一个'set_data'方法。 –

回答

3

下面是我用动画2D等高线图,它是从http://matplotlib.org/examples/animation/dynamic_image2.html

import pylab as pl 
import numpy as np 
import matplotlib.animation as animation 
import types 


fig = pl.figure() 
# Some 2D arrays to plot (time,x,y) 
data = np.random.random_sample((20,10,10)) 

# ims is a list of lists, each row is a list of artists to draw in the 
# current frame; here we are just animating one artist, the image, in 
# each frame 
ims = [] 
for i in range(len(data[:,0,0])): 
    t_step = int(i) 
    im = pl.contourf(data[i,:,:]) 

    ################################################################# 
    ## Bug fix for Quad Contour set not having attribute 'set_visible' 
    def setvisible(self,vis): 
     for c in self.collections: c.set_visible(vis) 
    im.set_visible = types.MethodType(setvisible,im) 
    im.axes = pl.gca() 
    im.figure=fig 
    #################################################################### 

    ims.append([im]) 

ani = animation.ArtistAnimation(fig, ims, interval=70, blit=False,repeat_delay=1000) 

pl.show() 
1

适应如果你像我和matplotlib.animation不起作用。这是你可以尝试的其他东西。如果要不断更新图中的颜色条和其他所有内容,请首先使用plt.ion()启用交互式绘图,然后使用plt.draw()和plt.clf()组合连续更新绘图。下面是示例代码:

import matplotlib.pyplot as plt 
import numpy as np 

plt.ion(); plt.figure(1); 
for k in range(10): 
    plt.clf(); plt.subplot(121); 
    plt.contourf(np.random.randn(10,10)); plt.colorbar(); 
    plt.subplot(122,polar=True) 
    plt.contourf(np.random.randn(10,10)); plt.colorbar(); 
    plt.draw(); 

注意这包含不同的次要情节和各类地块(即极性或笛卡尔)数字

3

我绘制的地理数据,因此需要底图。 基于由captain_Mhttps://github.com/matplotlib/matplotlib/issues/6139 我发表tacaswell,使您可以在2个维数据的动画使用contourf并将其保存为MP4,如果你有ffmpeg的启发响应的讨论/ bug报告了答案:

from matplotlib import animation 
from matplotlib import pyplot as plt 
import numpy as np 
from mpl_toolkits.basemap import Basemap 


fig, ax = plt.subplots() 

# set up map projection 
m = Basemap(projection='nsper',lon_0=-0,lat_0=90) 
m.drawcoastlines() 
m.drawparallels(np.arange(0.,180.,30.)) 
m.drawmeridians(np.arange(0.,360.,60.)) 

# some 2D geo arrays to plot (time,lat,lon) 
data = np.random.random_sample((20,90,360)) 
lat = np.arange(len(data[0,:,0])) 
lon = np.arange(len(data[0,0,:])) 
lons,lats = np.meshgrid(lon,lat) 

# ims is a list of lists, each row is a list of artists to draw in the 
# current frame; here we are animating three artists, the contour and 2 
# annotatons (title), in each frame 
ims = [] 
for i in range(len(data[:,0,0])): 
    im = m.contourf(lons,lats,data[i,:,:],latlon=True) 
    add_arts = im.collections 
    text = 'title={0!r}'.format(i) 
    te = ax.text(90, 90, text) 
    an = ax.annotate(text, xy=(0.45, 1.05), xycoords='axes fraction') 
    ims.append(add_arts + [te,an]) 

ani = animation.ArtistAnimation(fig, ims) 
## If you have ffmpeg you can save the animation by uncommenting 
## the following 2 lines 
# FFwriter = animation.FFMpegWriter() 
# ani.save('basic_animation.mp4', writer = FFwriter) 
plt.show() 
5

有一个简单的方法与FuncAnimation做到这一点: 您必须具有清除轴的功能,并绘制基于帧编号的新轮廓。不要忘记将blit设置为False

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.animation as animation 

DATA = np.random.randn(800).reshape(10,10,8) 


fig,ax = plt.subplots() 

def animate(i): 
     ax.clear() 
     ax.contourf(DATA[:,:,i]) 
     ax.set_title('%03d'%(i)) 
     return ax 

interval = 2#in seconds  
ani = animation.FuncAnimation(fig,animate,5,interval=interval*1e+3,blit=False) 

plt.show()