首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用contourf()制作动画?

如何使用contourf()制作动画?
EN

Stack Overflow用户
提问于 2014-04-14 21:00:05
回答 4查看 11.9K关注 0票数 15

我试图动画wigner函数的空间坐标,一些时间相关的数据。wigner函数是二维的,所以我使用contourf()绘制它。我将数据存储在一个HDF5文件中,可以动态地制作Wigner发行版,但我不知道如何将其动画化。我找到的所有动画教程和示例(例如this onethis one)都是严格针对线条情节的。具体来说,他们的animate(i)函数使用line.set_data(),而我似乎找不到与contourf()等价的东西。

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

contourf() set_data()**?**的是什么?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-07-15 17:10:26

使用FuncAnimation有一个简单的方法:您必须有一个函数来清除轴,并根据帧号绘制一个新的轮廓。不要忘记将blit设置为False

代码语言:javascript
复制
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)) 

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

plt.show()
票数 13
EN

Stack Overflow用户

发布于 2016-03-10 16:42:41

我正在绘制地理数据,因此需要基本地图。基于captain_M的回答和https://github.com/matplotlib/matplotlib/issues/6139上的讨论/bug报告,我发布了一个受tacaswell启发的响应,允许您在二维数据动画中使用contourf,如果有ffmpeg,则将其保存为mp4:

代码语言:javascript
复制
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()
票数 7
EN

Stack Overflow用户

发布于 2014-05-01 01:35:43

这是我用来动画2d轮廓图的东西,它是从image2.html改编的

代码语言:javascript
复制
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()
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23070305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档