Medulla Oblongata
Medulla Oblongata

Reputation: 3973

Animating contourf plot

I am trying to animate a contour plot. The following example is close enough to what I want to achieve (from this archive):

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 2 * np.pi, 0.1)
X,Y = np.meshgrid(x,x)
f1 = np.sin(X) + np.sin(Y)
f2 = np.cos(X) + np.cos(Y)

plt.figure()
C = plt.contourf(f1)
plt.show()

for coll in C.collections:
    plt.gca().collections.remove(coll)

C = plt.contourf(f2)
plt.draw()

However, there seems to be an issue with the remove command and I'm not sure how to fix it.

Upvotes: 1

Views: 136

Answers (1)

Falko
Falko

Reputation: 17942

You might want to add

plt.pause(0.1)

after the remove command. This makes matplotlib actually draw the plot up to this point and wait 0.1 seconds so that you can see something happening, before it continues with the next iteration.

Upvotes: 1

Related Questions