matplotlib 中带有 pcolormesh 例程的动画,如何初始化数据? [英] Animation with pcolormesh routine in matplotlib, how do I initialize the data?

查看:27
本文介绍了matplotlib 中带有 pcolormesh 例程的动画,如何初始化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 matplotlib 中的 pcolormesh 设置动画.我已经看到许多使用包动画的示例,其中大多数使用一维绘图例程,其中一些使用 imshow().首先,我想使用 FuncAnimation 例程.我的问题是,首先,我不知道是否可以初始化绘图

I am trying to animate a pcolormesh in matplotlib. I have seen many of the examples using the package animation, most of them using a 1D plot routine, and some of them with imshow(). First, I wan to use the FuncAnimation routine. My problem is, first, that I do not know if I can initialize the plot

fig,ax = plt.subplots()
quad = ax.pcolormesh(X,Y,Z)

我尝试了几行简单的代码:

I have tried a few simple lines:

fig,ax = plt.subplots()
quad = ax.pcolormesh([])

def init():
    quad.set_array([])
    return quad,

def animate(ktime):  
    quad.set_array(X,Y,np.sin(Z)+ktime)
return quad,

anim = animation.FuncAnimation(fig,animate,init_func=init,frames=Ntime,interval=200,blit=True)

plt.show()

顺便说一下,如何将标签设置为动画情节?如果标题显示随时间变化的数字,我可以为标题设置动画吗?谢谢

By the way, How do I set labels into and animated plot? Can I animate the title, if it is showing a number that changes in time? Thanks

推荐答案

问题是我错误地使用了 set_array() 例程.需要注意的是,您必须将一维数组传递给此例程,这一点非常重要.为此,关于该颜色、pcolormesh 等通常绘制多维数组,您应该使用 .ravel() .更重要的一件事:为了同时为不同的情节设置动画,animate.FuncAnimation 中的 blitz 选项必须是 False(请参阅部分此链接的动画选定的情节元素".

The problem was that I was wrongly using set_array() routine. It is very important to note that you must pass a 1D array to this routine. To do so, regarding that color, pcolormesh and so on usually plots multidimensional arrays, you should use .ravel() . One more important thing: In order to animate different plots at the same time, the blitz option at animate.FuncAnimation must be False (See section "Animating selected plot elements" of this link).

这里我发布了带有各种子图的简单程序的代码:

Here I post the code that simple program with various subplots:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
import matplotlib.animation as animation

y, x = np.meshgrid(np.linspace(-10, 10,100), np.linspace(-10, 10,100))

z = np.sin(x)*np.sin(x)+np.sin(y)*np.sin(y)

v = np.linspace(-10, 10,100)
t = np.sin(v)*np.sin(v)
tt = np.cos(v)*np.cos(v)
###########

fig = plt.figure(figsize=(16, 8),facecolor='white')
gs = gridspec.GridSpec(5, 2)
ax1 = plt.subplot(gs[0,0])

line, = ax1.plot([],[],'b-.',linewidth=2)
ax1.set_xlim(-10,10)
ax1.set_ylim(0,1)
ax1.set_xlabel('time')
ax1.set_ylabel('amplitude')
ax1.set_title('Oscillationsssss')
time_text = ax1.text(0.02, 0.95, '', transform=ax1.transAxes)

#############################
ax2 = plt.subplot(gs[1:3,0])
quad1 = ax2.pcolormesh(x,y,z,shading='gouraud')
ax2.set_xlabel('time')
ax2.set_ylabel('amplitude')
cb2 = fig.colorbar(quad1,ax=ax2)

#########################
ax3 = plt.subplot(gs[3:,0])
quad2 = ax3.pcolormesh(x, y, z,shading='gouraud')
ax3.set_xlabel('time')
ax3.set_ylabel('amplitude')
cb3 = fig.colorbar(quad2,ax=ax3)

############################
ax4 = plt.subplot(gs[:,1])
line2, = ax4.plot(v,tt,'b',linewidth=2)
ax4.set_xlim(-10,10)
ax4.set_ylim(0,1)

def init():
    line.set_data([],[])
    line2.set_data([],[])
    quad1.set_array([])
    return line,line2,quad1

def animate(iter):
    t = np.sin(2*v-iter/(2*np.pi))*np.sin(2*v-iter/(2*np.pi))
    tt = np.cos(2*v-iter/(2*np.pi))*np.cos(2*v-iter/(2*np.pi))
    z = np.sin(x-iter/(2*np.pi))*np.sin(x-iter/(2*np.pi))+np.sin(y)*np.sin(y)
    line.set_data(v,t)
    quad1.set_array(z.ravel())
    line2.set_data(v,tt)
    return line,line2,quad1

gs.tight_layout(fig)

anim = animation.FuncAnimation(fig,animate,frames=100,interval=50,blit=False,repeat=False)
plt.show()

print 'Finished!!'

这篇关于matplotlib 中带有 pcolormesh 例程的动画,如何初始化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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