使用Maltpoltlib对具有轮廓的pcolormesh进行动画处理 [英] Animating a pcolormesh with contours with maltpoltlib

查看:32
本文介绍了使用Maltpoltlib对具有轮廓的pcolormesh进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做某事:我需要及时地,表面上生动地说明热的进展.

I am working on something : I need to vizualize the progress of, let us say heat, in time and on a surface.

我在给带有轮廓的pcolormesh动画时遇到了一些麻烦:轮廓确实是动画的,但是pcolormesh却不是;我只会得到第一个永远不会被替换的.

I'm having some troubles animating a pcolormesh with contours : the contour is indeed animated, but the pcolormesh is not ; I only get the first one that is never replaced.

我的代码很像这样:

#x, y and z_temp are previously extracted from an Excel file
z=np.zeros(time,y,x)
for t in range(time):
    for m in range(len(y)):
        for n in range(len(x)):
            z[t][m][n] = z_temp[t]
x,y=np.meshgrid(x,y)

im = ax.contour(x, y, z[0], 20)
fond = ax.pcolormesh(x, y, z[0], cmap=cm.get_cmap('afmhot_r'))

def animate(t):
    print(t)
    ax.cla()
    fond = ax.pcolormesh(x, y, z[t], cmap=cm.get_cmap('afmhot_r'))
    im = ax.contour(x, y, z[t], 20, cmap=cm.get_cmap('cool'))
    return im, fond,

ani = anima.FuncAnimation(fig, animate, frames=time, interval=400, repeat_delay=1000)
plt.show()

我试图从这篇文章中获得启发:

I tried to get inspired by this post : Animation with pcolormesh routine in matplotlib, how do I initialize the data? as well as others that dealt with animation this time, but I seem incapable of combining the solutions I found. Following the solution of the link, I ended up having nothing at all...

有人可以帮忙吗?

也许我应该更具体地说明我的其他尝试.我将"def animate(t)"部分替换为仅获得轮廓但仍然具有动画效果的东西:

Maybe I should be more specific about my other attempt. I replaced the "def animate(t)" section by something that got me only the contour, but still animated :

def init():
    fond.set_array(np.array([]))
    return im, fond,
def animate(t):
    print(t) 
    ax.cla()  
    fond.set_array(z[t])  
    im = ax.contour(x, y, z[t], 20, cmap=cm.get_cmap('cool'))  
    return im, fond,

推荐答案

除了一些编码错误之外,我不明白为什么这在功能上不起作用.这个最小的示例同时更新了 pcolormesh contour .您可以测试一下是否适合您吗?

Beyond some coding errors, I don't see why this functionally wouldn't work. This minimal example updates both the pcolormesh and contour's without a problem. Could you test this to see if it works for you?

import numpy as np
import matplotlib.pylab as plt
import matplotlib.animation as animation

time = 10
x = np.linspace(0,5,6)
y = np.linspace(0,6,7)
z = np.random.random((time, y.size, x.size))

fig  = plt.figure()
ax   = plt.subplot(111)
im   = ax.contour(x, y, z[0], 20)
fond = ax.pcolormesh(x, y, z[0], cmap=plt.cm.get_cmap('afmhot_r'))

def animate(t):
    print(t)
    ax.cla()
    fond = ax.pcolormesh(x, y, z[t], cmap=plt.cm.get_cmap('afmhot_r'))
    im   = ax.contour(x, y, z[t], 20, cmap=plt.cm.get_cmap('cool'))
    return im, fond,

ani = animation.FuncAnimation(fig, animate, frames=time, interval=400, repeat_delay=1000)
plt.show()

这篇关于使用Maltpoltlib对具有轮廓的pcolormesh进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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