使用matplotlib中的pcolormesh例程动画,如何初始化数据?

Ale*_*oir 8 python animation matplotlib multidimensional-array

我想一个动画pcolormeshmatplotlib.我见过许多使用包动画的例子,其中大多数使用一维绘图例程,其中一些使用imshow().首先,我想使用FuncAnimation routine.我的问题是,首先,我不知道我是否可以初始化情节

fig,ax = plt.subplots()
quad = ax.pcolormesh(X,Y,Z)
Run Code Online (Sandbox Code Playgroud)

我尝试了几个简单的行:

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()
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*oir 9

问题是我错误地使用了set_array()常规.请务必注意,必须将1D数组传递给此例程.为此,关于该颜色,pcolormesh等通常绘制多维数组,您应该使用.ravel().更重要的是:为了同时为不同的绘图设置动画blitz,animate.FuncAnimation必须选择False(参见此链接的 "动画选定的绘图元素"部分).

在这里,我发布简单程序与各种子图的代码:

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!!'
Run Code Online (Sandbox Code Playgroud)


lum*_*ric 6

使用QuadMesh.set_array()时需要注意一个丑陋的细节.如果使用X,Y和C对QuadMesh进行实例化,则可以使用set_array()更新值C. 但是set_array不支持与构造函数相同的输入.阅读源表明,你需要通过一个一维数组,什么是更令人费解的是,这取决于shading 设置,您可能需要削减你的阵列 C.

编辑:甚至还有一个关于令人困惑的数组大小的错误报告shading='flat'.

这意味着:

使用带有着色='flat'的QuadMesh.set_array()

'flat'是默认值shading.

# preperation
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
y = np.linspace(-10, 10, num=1000)
x = np.linspace(-10, 10, num=1000)
X, Y = np.meshgrid(x, y)
C = np.ones((1000, 1000)) * float('nan')

# intantiate empty plot (values = nan)
pcmesh = plt.pcolormesh(X, Y, C, vmin=-100, vmax=100, shading='flat')

# generate some new data
C = X * Y

# necessary for shading='flat'
C = C[:-1, :-1]

# ravel() converts C to a 1d-array
pcmesh.set_array(C.ravel())

# redraw to update plot with new data
plt.draw()
Run Code Online (Sandbox Code Playgroud)

好像:

生成的图形与shadig = flat

请注意,如果省略,C = C[:-1, :-1]您将获得此图形:

破碎的图形与阴影=平坦

使用带有着色='gouraud'的QuadMesh.set_array()

# preperation (same as for 'flat')
import numpy as np
import matplotlib.pyplot as plt
plt.ion()
y = np.linspace(-10, 10, num=1000)
x = np.linspace(-10, 10, num=1000)
X, Y = np.meshgrid(x, y)
C = np.ones((1000, 1000)) * float('nan')

# intantiate empty plot (values = nan)
pcmesh = plt.pcolormesh(X, Y, C, vmin=-100, vmax=100, shading='gouraud')

# generate some new data
C = X * Y

# here no cut of of last row/column!

# ravel() converts C to a 1d-array
pcmesh.set_array(C.ravel())

# redraw to update plot with new data
plt.draw()
Run Code Online (Sandbox Code Playgroud)

如果你用shade ='gouraud'切掉最后一行/列,你会得到:

ValueError: total size of new array must be unchanged
Run Code Online (Sandbox Code Playgroud)


Dan*_*ein 2

我不知道为什么你的quad = ax.pcolormesh(X,Y,Z)函数给出错误。你能发布错误吗?

下面是我使用 pcolormesh 创建简单动画的方法:

import matplotlib.pyplot as plt
import numpy as np

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

z = np.sin(x**2+y**2)
z = z[:-1, :-1]

ax = plt.subplot(111)

quad = plt.pcolormesh(x, y, z)

plt.colorbar()

plt.ion()
plt.show()

for phase in np.linspace(0,10*np.pi,200):
    z = np.sin(np.sqrt(x**2+y**2) + phase)
    z = z[:-1, :-1]

    quad.set_array(z.ravel())
    plt.title('Phase: %.2f'%phase)
    plt.draw()

plt.ioff()
plt.show()
Run Code Online (Sandbox Code Playgroud)

其中一帧:在此输入图像描述

这有帮助吗?如果没有,也许你可以澄清这个问题。