Matplotlib Animation Outlines Update

I am looking for a way to update my contour lines in an animation that does not require me to change my shape every time.

Most of the answers to this question I found a lawyer, recalling ax.contour, but since my contours overlap with another image, it is unbearably slow.

The only answer I found that is close to answering a question answers it with a dead link: Contour animation in matplotlib using FuncAnimation

Code example:

#!/usr/bin/env python

import matplotlib.pylab as plt
import matplotlib.animation as anim
from matplotlib.colors import LinearSegmentedColormap as lsc
import numpy

#fig = 0; ax = 0; im = 0; co = 0


image_data = numpy.random.random((100,50,50))
contour_data = numpy.random.random((100,50,50))

def init():
    global fig, ax, im, co
    fig = plt.figure()
    ax = plt.axes()
    im = ax.imshow(image_data[0,:,:])
    co = ax.contour(contour_data[0,:,:])

def func(n):
    im.set_data(image_data[n,:,:])
    co.set_array(contour_data[n,:,:])

init()
ani = anim.FuncAnimation(fig, func, frames=100)
plt.show()

Greetings.

+4
source share
1 answer

, ; , , / . :

set_array() ( ) colormapping . , , . , C.remove(), - ( ). :

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() 

, , .

0

Source: https://habr.com/ru/post/1537748/


All Articles