Search code examples
pythonpython-3.xmatplotlibanimationmatplotlib-animation

Matplotlib animation: update axis ticks in pcolormesh


I'm creating an animation of an image with axis values varying over time.

All_data is a matrix that contains in one axe time frames inside there are 3 quatities which are longitude latitude and pixel values so

lon = All_data[0,0] #-> lon is a 2D nxm matrix
lat = All_data[0,1] #-> lat is a 2D nxm matrix
I = All_data[0,2]    #-> I   is a 2D nxm matrix
im = axis.pcolormesh(lon,lat,I)
def animate(i):
    im.set_array(All_data[i,2])
    #code to add here to update axis too
    return im

ani = FuncAnimation.....etc

The problem is that this code will not update my X and Y (longitude and attitude) axis. is there any way to update the axis values with images using pcolor or pcolormesh or even imshow? The results are shown in the image as you see the values of the axis are not changingenter image description here


Solution

  • I haven't tested this with your code (the question could use an MWE), but you might be able to do something like:

    # Form relative steps in coordinates between frames for each frame
    dlon = np.r_[0, All_data[1:, 0] - All_data[:-1, 0]]
    dlat = np.r_[0, All_data[1:, 1] - All_data[:-1, 1]]
    
    def animate(i):
        im.set_array(All_data[i, 2])
        new_coords = im.properties()['coordinates']  # N x M x 2 array
        new_coords[..., 0] += dlon[i]
        new_coords[..., 1] += dlat[i]
        im._coordinates = new_coords  # is there a public interface?
    
        return im