简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM