2014-04-14 29 views
9

Tôi đang cố gắng tạo hoạt ảnh cho hàm wigner của các tọa độ không gian của một số dữ liệu phụ thuộc vào thời gian. Hàm wigner là 2 chiều, vì vậy tôi sử dụng contourf() để vẽ nó. Tôi có các dữ liệu được lưu trữ trong một tập tin HDF5 và có thể làm cho Wigner phân phối trên bay, nhưng tôi không thể tìm ra cách để animate nó. Tất cả các hướng dẫn hoạt hình và các ví dụ tôi đã có thể tìm thấy (ví dụ: this onethis one) là hoàn toàn phù hợp với các ô dòng. Cụ thể, chức năng animate(i) của chúng tôi sử dụng line.set_data() và tôi dường như không thể tìm thấy tương đương cho contourf().Làm cách nào để tạo hoạt ảnh với đường viền()?

Tôi làm cách nào để tạo hình ảnh được tạo bằng contourf()?

Số contourf() tương đương với set_data() là gì?

+0

thể trùng lặp của [Sử dụng matplotlib.animate animate một âm mưu đường viền trong python] (http://stackoverflow.com/questions/16915966/using-matplotlib-animate-to-animate-a-contour-plot -in-python) – Dan

+0

Những gì tôi đã làm là tạo ra một ô đường bao mới mỗi lần, và để ẩn cái cũ tôi làm 'cont.set_alpha (0)'. Hacky. Phải có một phương thức 'set_data'. –

Trả lời

3

Đây là những gì tôi sử dụng để animate lô đường viền 2d, nó được chuyển thể từ 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

Nếu bạn đang như tôi và matplotlib.animation không hoạt động. Đây là cái gì khác bạn có thể thử. Nếu bạn muốn cập nhật liên tục thanh màu và mọi thứ khác trong hình, hãy sử dụng lệnh plt.ion() ngay từ đầu để bật âm mưu tương tác và sử dụng tổ hợp plt.draw() và plt.clf() để liên tục cập nhật cốt truyện . Dưới đây là mã ví dụ:

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(); 

Lưu ý rằng điều này làm việc với các con số có chứa ô phụ khác nhau và các loại khác nhau của lô (ví dụ cực hay Descartes)

3

Tôi đang âm mưu dữ liệu địa lý và do đó cần bản đồ cơ sở. Dựa trên câu trả lời của captain_M và một báo cáo thảo luận/lỗi trên https://github.com/matplotlib/matplotlib/issues/6139 tôi gửi một phản ứng lấy cảm hứng từ tacaswell cho phép bạn sử dụng contourf trong một phim hoạt hình của 2 dữ liệu chiều và lưu nó như mp4 nếu bạn có 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() 
5

có một cách đơn giản để làm điều đó với FuncAnimation: Bạn phải có một chức năng mà xóa trục và vẽ một đường viền mới dựa trên số khung. Đừng quên đặt blitFalse.

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() 
Các vấn đề liên quan