如何使用contourf()制作动画? [英] How can I make an animation with contourf()?

查看:36
本文介绍了如何使用contourf()制作动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为某些时间相关数据的空间坐标的 wigner 函数设置动画.wigner 函数是二维的,所以我使用 contourf() 来绘制它.我将数据存储在 HDF5 文件中,可以即时制作 Wigner 分布,但我不知道如何为其设置动画.我能找到的所有动画教程和示例(例如 这个这个)严格用于线图.具体来说,他们的 animate(i) 函数使用 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()?

contourf()set_data() 的等价物是什么?

What's the contourf() equivalent of set_data()?

推荐答案

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

There's a simple way to do it with FuncAnimation: You must have a function that clears the axis and plot a new contour based on frame number. Don't forget to set blit as 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)) 

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

plt.show()

这篇关于如何使用contourf()制作动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆