15

時間依存データの空間座標のウィグナー関数をアニメーション化しようとしています。ウィグナー関数は 2 次元なので、contourf() を使用してプロットしています。HDF5 ファイルに保存されたデータがあり、その場で Wigner 分布を作成できますが、アニメーション化する方法がわかりません。私が見つけたすべてのアニメーションのチュートリアルと例 (たとえば、これこれ) は、厳密には線プロット用です。具体的には、それらのanimate(i)関数は を使用しておりline.set_data()、 に相当するものを見つけることができないようですcontourf()

で作成した画像をアニメーション化するにはどうすればよいcontourf()ですか?

contourf()に相当するものは何set_data()ですか?

4

4 に答える 4

7

地理データをプロットしているため、Basemap が必要です。Captain_Mの回答とhttps://github.com/matplotlib/matplotlib/issues/6139のディスカッション/バグ レポートに基づいて、 tacaswellに 触発された応答を投稿し、2 次元データのアニメーションで輪郭を使用して保存できるようにしますffmpeg がある場合は mp4 として:

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()
于 2016-03-10T16:42:41.270 に答える
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();

これは、さまざまなサブプロットとさまざまなタイプのプロット (つまり、極座標またはデカルト座標) を含む図で機能することに注意してください。

于 2015-07-05T04:39:00.330 に答える