Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9077719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:26:29+00:00 2026-06-16T19:26:29+00:00

I am trying to animate contours in matplotlib 1.1.0 Based on: 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

  • 0

I am trying to animate contours in matplotlib 1.1.0

Based on:
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 and
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) Is this the best way to do this? Mention otherwise.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:26:32+00:00Added an answer on June 16, 2026 at 7:26 pm

    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.

    Tweaking your code a bit:

    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,
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to animate a Canvas-based texture that is mapped onto a plane, like
I'm trying to animate the ScaleY property of a LayoutTransform based on a DataTrigger
I'm trying to animate the appearance of a segment of a circle. To archive
I´m trying to animate a sort of draggable UIView where the behaviour should be
I'm trying to animate the div within an list item with a click event
I'm trying to animate the locations of a CAGradientLayer to shrink when the UIScrollView
I am trying to animate the transform property of a view. Here is my
I'm trying to animate a logo. The markup looks like this: <div id=logo> <h1>
I am trying to animate the blinking between two images on an imageview to
I am trying to animate sliding out one view from a window and simultaneous

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.