轮廓动画matplotlib [英] Animation with contours matplotlib

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

问题描述

我正在尝试在matplotlib 1.1.0中对轮廓进行动画处理

I am trying to animate contours in matplotlib 1.1.0

基于: http://www.mail-archive.com/matplotlib-users@lists. sourceforge.net/msg17614.html http://matplotlib.1069221.n5. nabble.com/Matplotlib-1-1-0-animation-vs-contour-plots-td18703.html http://www.scipy.org/Cookbook/Matplotlib/Gridding_irregularly_spaced_data

from numpy import linspace,exp,vstack
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from numpy.random import uniform, seed

def main():
    seed(1234)
    x = uniform(-2,2,100)
    y = uniform(-2,2,100)
    data = vstack((x*exp(-x**2-y**2),0.5*x*exp(-x**2-y**2),0.2*x*exp(-x**2-y**2)))
    xi = linspace(min(x), max(x))
    yi = linspace(min(y), max(y))
    zi = []
    numframes = data.shape[0]
    for ii in range(numframes):
        zi.append(griddata((x, y), data[ii], (xi[None,:], yi[:,None]), method='cubic'))

    fig = plt.figure()
    im = plt.contour(xi, yi, zi[0], 15, linewidths=0.5, colors='k')
    ani = animation.FuncAnimation(fig, update_contour_plot, frames=xrange(numframes), fargs=(zi, im, fig, xi, yi), interval=100)
    plt.colorbar()
    plt.show()

def update_contour_plot(i, data, im, fig, xi, yi):
    for coll in im.collections:
        try:
            plt.gca().collections.remove(coll)
        except ValueError: #Everything is not removed for some reason!                                                                                      
            pass
    im = plt.contour(xi, yi, data[i], 15, linewidths=0.5, colors='k')
    plt.title(str(i))
    return im,

main()

1)这是执行此操作的最佳方法吗?否则提及.

1) Is this the best way to do this? Mention otherwise.

2)在最终输出中,前一帧的轮廓线仍然可见.如何删除它们?参见ValueValue除外:

2) In the final output, contour lines from previous frame are still visible. How do I remove them? See except ValueError:

推荐答案

您存在范围问题,您在更新程序中创建的新im不会被传递出去,因此第二次尝试通过循环再次删除第一行行,它正确地抱怨它不能删除,因为您已经删除了它们.

You have a scoping problem, the new im you create in your updater is not being passed back out, so the second time through the loop you are trying to remove the first layer of lines again, which it rightly complains it can't because you already removed them.

稍微调整一下代码:

def main():
    seed(1234)
    x = uniform(-2,2,100)
    y = uniform(-2,2,100)
    data = vstack((x*exp(-x**2-y**2),0.5*x*exp(-x**2-y**2),0.2*x*exp(-x**2-y**2)))
    xi = linspace(min(x), max(x))
    yi = linspace(min(y), max(y))
    zi = []
    numframes = data.shape[0]
    for ii in range(numframes):
        zi.append(griddata((x, y), data[ii], (xi[None,:], yi[:,None]), method='cubic'))

    fig = plt.figure()
    im = plt.contour(xi, yi, zi[0], 15, linewidths=0.5, colors='k')
    ax = fig.gca()
    ani = animation.FuncAnimation(fig, update_contour_plot, frames=xrange(numframes), fargs=(zi, ax, fig, xi, yi), interval=100)
    plt.colorbar(im)
    plt.show()
    return ani


def update_contour_plot(i, data,  ax, fig, xi, yi):
    ax.cla()
    im = ax.contour(xi, yi, data[i], 15, linewidths=0.5, colors='k')
    plt.title(str(i))
    return im,

另一种选择是使im某种可变(例如将其包装在字典中),以便您的更改将逐帧传播.

Another option is to make im a mutable of some sort (like wrap it in a dictionary) so that your changes will propagate from frame to frame.

这篇关于轮廓动画matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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