问题描述
我试图动画一些时间相关的数据的空间坐标的维格纳功能。维格纳功能是二维的,所以我用contourf()来绘制。我存储在一个HDF5文件中的数据,并可以实时做出维格纳分布,但我不能想出如何制作动画。所有的动画教程和示例我已经能够找到的(例如和)是严格的线图。具体来说,他们的动画(我)
函数使用 line.set_data()
,我似乎无法找到一个相当于 contourf()
。
I'm trying to animate the wigner function of the spatial coordinates of some time-dependent data. The wigner function is 2 dimensional, so I'm using contourf() to plot it. I have the data stored in a HDF5 file and can make Wigner distributions on the fly, but I can't figure out how to animate it. All of the animation tutorials and examples I've been able to find (for example this one and this one) are strictly for line plots. Specifically, their animate(i)
function uses line.set_data()
, and I can't seem to find an equivalent for contourf()
.
如何可以动画contourf()与制作图片
?
How can I animate images made with contourf()
?
什么是<$ C C> contourf()相当于<$ç$ C> set_data()
What's the contourf()
equivalent of set_data()
?
推荐答案
我绘制地理数据,因此需要底图。
基于答案的 captain_M 的和
我发表的启发响应的 tacaswell 的,可以让你在2维数据的动画使用contourf并将其保存为MP4,如果你有ffmpeg的:
I am plotting geographical data and therefore need Basemap.Based on the answer by captain_M and a discussion/bug report on https://github.com/matplotlib/matplotlib/issues/6139I post a response inspired by tacaswell that allows you to use contourf in an animation of 2 dimensional data and save it as mp4 if you have ffmpeg:
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()
这篇关于我怎样才能使与contourf动画()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!