Update tripcolor graph in matplotlib animation

ghz 9hours ago ⋅ 1 views

I have been trying to create an animation in matplotlib from the graph tripcolor. Let's say I have

field = ax.tripcolor(tri, C)

How do I change the value of C after each iteration of the animation?

Many thanks,

Answer

To create an animation with matplotlib's tripcolor plot, you can update the values of C (which represents the color values of the triangles) after each iteration using the FuncAnimation class from matplotlib.animation. You can achieve this by modifying the C array and updating the plot accordingly.

Here’s how you can do it:

Step-by-Step Approach

  1. Define the tripcolor plot: Create the initial plot using tripcolor.
  2. Set up the animation function: This function will update the values of C in each iteration.
  3. Use FuncAnimation to animate: Update the plot at each frame.

Example Code

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.tri import Triangulation

# Create some dummy data for the triangulation
n_points = 100
x = np.random.rand(n_points)
y = np.random.rand(n_points)
triangles = np.random.randint(0, n_points, (n_points - 2, 3))  # Triangular mesh

# Create the Triangulation object
tri = Triangulation(x, y, triangles)

# Initial values for the 'C' array (color values for the tripcolor)
C = np.random.rand(len(tri.triangles))  # Random initial values for color

# Set up the figure and axis
fig, ax = plt.subplots()
field = ax.tripcolor(tri, C, cmap='viridis')

# Animation update function
def update(frame):
    global C  # Access the color data globally
    C = np.random.rand(len(tri.triangles))  # Update C with new random values
    field.set_array(C)  # Set the new color data
    return field,  # Return the updated field for animation

# Create the animation
ani = FuncAnimation(fig, update, frames=100, interval=100, blit=True)

# Show the plot
plt.show()

Key Points:

  • Initial Plot (tripcolor): The plot is created with ax.tripcolor(tri, C) where C is the initial color array. You can set it to any values, including random or a pre-defined function.

  • Updating the Color Array (C): Inside the update function, the C array is updated in each frame. In this example, I simply regenerate random values for C, but you can modify it based on your needs (e.g., using a mathematical function or any data-driven approach).

  • set_array Method: field.set_array(C) updates the color data for the plot. This method updates the colors of the existing tripcolor plot without redrawing the entire figure.

  • Animation (FuncAnimation): This class is used to animate the plot. It takes the figure (fig), the update function (update), and other animation parameters like frames, interval, and blit.

Notes:

  • blit=True: This optimizes the animation by only redrawing parts of the figure that have changed (in this case, only the color data).
  • You can adjust the color updates in C to match the specific animation you need (e.g., changing values based on time or another input).

With this approach, you'll be able to smoothly animate the tripcolor plot by modifying the color array (C) after each iteration.