Python: matplotlib - loop clear and showing different plots over the same drawing

I want to see how the graph changes with different values ​​using a loop. I want to see him on the same plot. But I do not want to see the remains of the previous plot in the picture. In MATLAB, this is possible by creating a shape and simply drawing over the same figure. Closing it at the end of the cycle.

how

fh = figure();
%for loop here
%do something with x and y    
subplot(211), plot(x);
subplot(212), plot(y); 
pause(1)
%loop done
close(fh);

      

I cannot find an equivalent to this in matplotlib. Usually all the questions are about plotting different series on the same plot, which seems to naturally arise on matplotlib, by plotting multiple series using plt.plot()

and then showing them everything finally using plt.show()

. But I want to refresh the plot.

+3


source to share


2 answers


There are two different ways to create animations in matplotlib

interactive mode

Enabling more interactive is done with plt.ion()

. This will create a plot, although it show

hasn't been called yet. The plot can be updated by calling plt.draw()

or animation plt.pause()

.

import matplotlib.pyplot as plt

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,17)
ax1.set_ylim(-400,3000)
plt.ion()

for i in range(15):
    x.append(x[-1]+x[-2])
    line1.set_data(range(len(x)), x)
    y.append(y[-1]+y[-2])
    line2.set_data(range(len(y)), y)

    plt.pause(0.1)

plt.ioff()    
plt.show()

      

FuncAnimation



Matplotlib provides an animation submodule that makes it easy to create animations and also makes them easy to save. Same as above, using FuncAnimation

will look like this:

import matplotlib.pyplot as plt
import matplotlib.animation

x = [1,1]
y = [1,2]

fig, (ax1,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True)
line1, = ax1.plot(x)
line2, = ax2.plot(y)
ax1.set_xlim(-1,18)
ax1.set_ylim(-400,3000)


def update(i):
    x.append(x[-1]+x[-2])
    line1.set_data(range(len(x)), x)
    y.append(y[-1]+y[-2])
    line2.set_data(range(len(y)), y)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=14, repeat=False)   
plt.show()

      

An example of animation of a sine wave with varying frequency and its power spectrum would be the following:

import matplotlib.pyplot as plt
import matplotlib.animation
import numpy as np

x = np.linspace(0,24*np.pi,512)
y = np.sin(x)

def fft(x):
    fft = np.abs(np.fft.rfft(x))
    return fft**2/(fft**2).max()

fig, (ax1,ax2) = plt.subplots(nrows=2)
line1, = ax1.plot(x,y)
line2, = ax2.plot(fft(y))
ax2.set_xlim(0,50)
ax2.set_ylim(0,1)

def update(i):
    y = np.sin((i+1)/30.*x)
    line1.set_data(x,y)
    y2 = fft(y)
    line2.set_data(range(len(y2)), y2)

ani = matplotlib.animation.FuncAnimation(fig, update, frames=60, repeat=True)  
plt.show()

      

enter image description here

+2


source


If you call plt.show()

inside a loop, you will see a graph for each element of the loop if you close the window containing the shape. The process will be displayed for the first item, then if you close the window, you will see a graph for the second item in the loop, etc.



0


source







All Articles