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

  • SEARCH
  • Home
  • 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 7903795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:50:15+00:00 2026-06-03T09:50:15+00:00

I have a NxN grid with some values, which change every time step. I

  • 0

I have a NxN grid with some values, which change every time step. I have found a way to plot a single grid configuration of this with matshow function, but I don’t know how do I update the status with every time step. Here is a simple example:

from pylab import *
from matplotlib import pyplot

a = arange(25)
a = a.reshape(5,5)
b = 10*rand(5,5)
matshow(a-b, cmap = cm.jet)
colorbar()
show()

This code produces the following picture:
enter image description here
Now imagine that the next time step some values change, so should this picture. This is the logic I had in mind:

from pylab import *
from matplotlib import pyplot

a = arange(25)
a = a.reshape(5,5)
time=10
for t in range(time):
    b = 10*rand(5,5)
    print b
    matshow(a-b, cmap=cm.jet)
    colorbar()
show()

This produces 10 pictures. I’d like to animate this instead producing individual pictures, and for example I’d like to choose a time step between changes (that is, frame rate).
Also, I’m open to suggestions for a different functions, if matshow is not the way to go, but please keep it simple, I’m relatively inexperienced.

  • 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-03T09:50:16+00:00Added an answer on June 3, 2026 at 9:50 am

    matplotlib 1.1 has an animation module (look at the examples).

    Using animation.FuncAnimation you can update your plot like so:

    import numpy as np
    import matplotlib.pyplot as plt 
    import matplotlib.animation as animation
    
    def generate_data():
        a = np.arange(25).reshape(5, 5)
        b = 10 * np.random.rand(5, 5)
        return a - b 
    
    def update(data):
        mat.set_data(data)
        return mat 
    
    def data_gen():
        while True:
            yield generate_data()
    
    fig, ax = plt.subplots()
    mat = ax.matshow(generate_data())
    plt.colorbar(mat)
    ani = animation.FuncAnimation(fig, update, data_gen, interval=500,
                                  save_count=50)
    plt.show()
    

    You can save the animation using:

    ani.save('animation.mp4')
    

    I you save it with

    ani.save('animation.mp4', clear_temp=False)
    

    the frames are conserved and you can create an animated gif like the following with

    convert *.png animation.gif
    

    enter image description here

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

Sidebar

Related Questions

Suppose you have an NxN grid, and you want to visit every cell exactly
I have a NxN grid of values and a 2xM list of x,y data
Lets say you have a GridLayout of JButtons in an NxN grid, in code
I have a NxN matrix which I want to split into non-overlap KxK block.
I have an array of size n^2 representing a square grid of size nxn.
I have a grid of NxN cells (think of a 2 dimensional array defined
I have an NxN 2D array implemented as a 1D array using A[N*i+j]. I
Suppose you have an NxN matrix and you have to check whether it's a
I have a set of N objects, and I'd like to compute a NxN
I have a large matrix from which I would like to gather a collection

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.