how to get a live updating graph line - animation

i'm trying to create a program which reads some randomly generated data, the xax and yax lists, as plots them live as it reads it in an animated fashion. Here is the code:
from matplotlib.pylab import *
import time, random
ion()
xax = []
yax = []
axes
for j in range (0,20):
xax.append(j)
r = random.randrange(0, 20)
yax.append(r)
maxx = max(xax)
maxy = max(yax)
print maxx, maxy
axis([0,maxx,0,maxy])
line, = plot(xax[0],yax[0])
draw()
for i in xax:
print i, yax[i]
line.set_ydata(yax[i])
draw()

Following the discussion in the comments to the question and in chat, try the following to update a line, extending it for each iteration of a loop. The problem in the original code was that only one point at a time was being draw, so obviously no line was drawn to screen. In the following I extend the x and y data at each iteration of the loop using numpy.append (part of matplotlib.pylab).
from matplotlib.pylab import *
import time, random
ion()
xax = range(0, 20)
yax = [random.randrange(0, 20) for _ range(0, 20)]
axes()
xlim(xmax=max(xax))
ylim(ymax=max(yax))
line, = plot(xax[0], yax[0])
for i, x in enumerate(xax):
line.set_ydata(append(line.get_ydata(), yax[i]))
line.set_xdata(append(line.get_xdata(), x))
draw()
time.sleep(1.)

Related

Calculating kernel-density estimate in parallel

I want to perform kernel-density estimate on a grid in parallel. There is some scaling involved and I can't figure out how to obtain the same results when performing the calculation on different threads.
To illustre this, I'm using an simple example from the scipy documentation.
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
def measure(n):
"Measurement model, return two coupled measurements."
m1 = np.random.normal(size=n)
m2 = np.random.normal(scale=0.5, size=n)
return m1+m2, m1-m2
m1, m2 = measure(2000)
xmin = m1.min()
xmax = m1.max()
ymin = m2.min()
ymax = m2.max()
kde_bandwith = 0.5
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([m1, m2])
kernel = stats.gaussian_kde(values, bw_method=kde_bandwith)
Z = np.reshape(kernel.evaluate(positions).T, X.shape)
I'm performing the last two lines on n_split subsets of the data (in practice this would be performed on different thread but here I kept a simple loop for simplicity):
## split
n_split = 10
values_split = np.array_split(values, n_split, axis=1)
results = []
for v in values_split:
kernel_n = stats.gaussian_kde(v, bw_method=kde_bandwith)
Z_n = np.reshape(kernel_n.evaluate(positions).T, X.shape)
results.append(Z_n)
Z2 = np.sum(results, axis=0)
here I'm simply recombining the results from each subset of data by summing the Z_n of each subset together into Z2.
Finally, a simple code to compare the results: left is the original version, middle is the result obtain from combining the subsets, and right the difference between the two.
fig = plt.figure(figsize=(9,2), dpi=200)
ax1 = fig.add_subplot(1,3,1)
ax2 = fig.add_subplot(1,3,2)
ax3 = fig.add_subplot(1,3,3)
c1 = ax1.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax])
#ax1.plot(m1, m2, 'k.', markersize=2)
ax1.set_xlim([xmin, xmax])
ax1.set_ylim([ymin, ymax])
plt.colorbar(c1, shrink=0.6)
c2 = ax2.imshow(np.rot90(Z2), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax])
#ax2.plot(m1, m2, 'k.', markersize=2)
ax2.set_xlim([xmin, xmax])
ax2.set_ylim([ymin, ymax])
plt.colorbar(c2, shrink=0.6)
c3 = ax3.imshow(np.rot90(Z2-Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax])
ax3.plot(m1, m2, 'k.', markersize=0.1)
ax3.set_xlim([xmin, xmax])
ax3.set_ylim([ymin, ymax])
plt.colorbar(c3, shrink=0.6)
plt.show()
Notes: It looks like the magnitude of the KDE is n_split larger, but by simply normalizing with 1/n_split or even len(values_split)/len(values) (which in this case is equal), the results are not exactly the same, shown on the next figure as reference.

How do I modify my matplotlib FuncAnimation init_func to remove artifacts that were introduced by using twinx?

Summary: I created a script for drawing strip charts with the matplotlib animation framework to support multiple y-axes using the Axes.twinx(), which resulted in an artifact on the plot which I can't remove. I think I need to modify the animation init_func to account for the multiple axes.
Python 2.7, matplotlib 2.0.2 with the Qt backend, and conda 4.3.18, running on Ubuntu Linux 17.04.
Full Description: I created strip chart drawing program based on the matplotlib strip chart demo:
https://matplotlib.org/examples/animation/strip_chart_demo.html
I modified it to help some EE's on a project including rewriting it in a more procedural style so they were more comfortable working with it (not sure how successful that was, we're still talking about it), adding support for multiple lines, and changed it so it was continuously scrolling left.
The script includes a makeChart() function that creates a series of Line2D's in a loop, and adds them to a list, and returns them to the caller:
lines = []
for iline in range(0,linesPerPlot):
lines.append(makeLine(ax, maxt, dt, ymin, ymax, colors[iline % len(colors)]))
return lines
When I added multiple lines there was an artifact that appeared when running with blit=True that I got rid of by adding an init_function.
The init function is simple and looks like this:
def initDisplay(lines):
"""Init display."""
return lines
When the script executes makeChart() is called and returns a list of Line2D's that are being plotted, it creates a lambda that wraps the init function that is then passed to FuncAnimation:
lines = makeChart(ax, secondsPerPlot, secondsPerSample, linesPerPlot, ymin, ymax)
...
init = lambda: initDisplay(lines)
ani = animation.FuncAnimation(fig, update, emitter, init_func=init, interval=millisPerFrame, blit=True)
The result works reasonable well:
And an example with that displays some generated sin waves is here:
https://gist.github.com/mdkrajnak/f7cfd3f720453d53da4a80fa45df3b66
Later I made an additional modification so that each line had an independent y-axis by using Axes.twinx. After the modification there's now an artifact that I cannot remove that appears to be left over from the first time the first line is rendered.
The new inner loop in makeChart() looks like:
lines = []
lines.append(makeLine(ax, maxt, dt, ymin, ymax, colors[0]))
for iline in range(1,linesPerPlot):
twin_ax = ax.twinx()
lines.append(makeLine(twin_ax, maxt, dt, ymin, ymax, colors[iline % len(colors)]))
return lines
And the full code is here:
https://gist.github.com/mdkrajnak/e8b37300545f3ffea651d628933bd0ee
I tried modifying the init function so that it returned a list with both the lines and axes:
def initDisplay(lines, axs):
"""Init display."""
return lines + axs
And the makeChart() function so it returned the axes along with the lines in the sequence of artists that it returned:
lines = []
axs = []
# Add first line, then add subsequent lines sharing the x-axis.
lines.append(makeLine(ax, maxt, dt, ymin, ymax, colors[0]))
axs.append(ax)
for iline in range(1,linesPerPlot):
twin_ax = ax.twinx()
lines.append(makeLine(twin_ax, maxt, dt, ymin, ymax, colors[iline % len(colors)]))
axs.append(twin_ax)
return lines, axs
The full code is here:
https://gist.github.com/mdkrajnak/e6eaca509cd8321b9b56a4d25c3e1e80
But this version fails with "AttributeError: draw_artist can only be used after an initial draw which caches the render"
File "/home/mdk/opt/miniconda3/envs/p2/lib/python2.7/site-packages/matplotlib/animation.py", line 1123, in _post_draw self._blit_draw(self._drawn_artists, self._blit_cache)
File "/home/mdk/opt/miniconda3/envs/p2/lib/python2.7/site-packages/matplotlib/animation.py", line 1138, in _blit_draw a.axes.draw_artist(a)
File "/home/mdk/opt/miniconda3/envs/p2/lib/python2.7/site-packages/matplotlib/axes/_base.py", line 2441, in draw_artist raise AttributeError(msg)
AttributeError: draw_artist can only be used after an initial draw which caches the render
My thinking is still that the init function needs to return the axes along with the lines, but I need to somehow cause an initial draw of the axes before the init function is called. Is there something I can to to prerender the axes, or is there something else I need to do?
It seems that blitting is performed per axes. So it might be that the procedure is
for ax in all axes:
get axes background
draw line
This means that the first line is part of the background from the second axes and as such will be part of every successive frame.
The only solution I can think of at the moment is to make the lines invisible until the backgrounds of all axes have been stored for blitting.
line = Line2D(tdata, ydata, color=color, visible=False)
Only after the first call to updateLines turn them visible again.
n = [0]
def updateLines(lines, arrays):
"""Update individual lines and return a sequence of artists to the animator."""
artists = []
for iline in range(len(lines)):
artists.append(updateLine(lines[iline], arrays[iline]))
if n[0] > 0:
lines[iline].set_visible(True)
n[0] += 1
return artists
Complete code:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.lines import Line2D
import math
# Initalize script constants
ymin = -1.1
ymax = 1.1
linesPerPlot = 3
samplesPerFrame = 1
framesPerSecond = 20
secondsPerPlot = 5
# Calculate dependent constants
samplesPerSecond = samplesPerFrame * framesPerSecond
samplesPerPlot = samplesPerSecond * secondsPerPlot
secondsPerSample = 1.0/samplesPerSecond
millisPerFrame = 1000.0/framesPerSecond
# Define core functions
def makeLine(ax, maxt, dt, ymin, ymax, color):
"""Make an empty Line2D for the initial chart."""
nvalues = int(round(maxt/dt))
tdata = [dt*tm for tm in range(nvalues)]
ydata = [0 for tm in range(nvalues)]
line = Line2D(tdata, ydata, color=color, visible=False) ### <- visible false
ax.add_line(line)
ax.set_ylim(ymin, ymax)
return line
def makeChart(ax, maxt, dt, linesPerPlot, ymin, ymax):
"""Make a chart and return a list of the lines it contains."""
colors = [ 'r', 'b', 'g', 'k' ]
lines = []
# Add first line, then add subsequent lines sharing the x-axis.
lines.append(makeLine(ax, maxt, dt, ymin, ymax, colors[0]))
for iline in range(1,linesPerPlot):
twin_ax = ax.twinx()
lines.append(makeLine(twin_ax, maxt, dt, ymin, ymax, colors[iline % len(colors)]))
ax.set_xlim(0, maxt)
return lines
def initDisplay(lines):
"""Init display."""
return lines
def updateLine(line, ys):
"""Update the data in one line, popping off the last value."""
tdata, ydata = line.get_data()
for y in ys:
ydata.append(y)
ydata.pop(0)
line.set_data(tdata, ydata)
return line
n = [0]
def updateLines(lines, arrays):
"""Update individual lines and return a sequence of artists to the animator."""
artists = []
for iline in range(len(lines)):
artists.append(updateLine(lines[iline], arrays[iline]))
if n[0] > 0:
lines[iline].set_visible(True)
n[0] += 1
return artists
def emitData(linesPerPlot, samplesPerFrame):
"""Create the data that will be plotted."""
nsample = 0
while True:
samples = [[] for i in range(linesPerPlot)]
for isample in range(samplesPerFrame):
nsample = nsample + 1
for iline in range(linesPerPlot):
pi_increment = (math.pi/(10.0 * (iline+1)))
samples[iline].append(math.sin(nsample * pi_increment))
yield samples
# Make chart.
fig, ax = plt.subplots()
lines = makeChart(ax, secondsPerPlot, secondsPerSample, linesPerPlot, ymin, ymax)
# Start the animator.
update = lambda samples: updateLines(lines, samples)
emitter = lambda: emitData(linesPerPlot, samplesPerFrame)
init = lambda: initDisplay(lines)
ani = animation.FuncAnimation(fig, update, emitter, init_func=init, interval=millisPerFrame, blit=True)
plt.show()

How do I plot two animations in a single plot with matplotlib?

In the following code I have two separate animations and I have plotted them in a two separate subplots. I want both of them to run in a single plot instead of this. I have tried the approach explained below but it is giving me issues as explained below. Please help
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time as t
x = np.linspace(0,5,100)
fig = plt.figure()
p1 = fig.add_subplot(2,1,1)
p2 = fig.add_subplot(2,1,2)
def gen1():
i = 0.5
while(True):
yield i
i += 0.1
def gen2():
j = 0
while(True):
yield j
j += 1
def run1(c):
p1.clear()
p1.set_xlim([0,15])
p1.set_ylim([0,100])
y = c*x
p1.plot(x,y,'b')
def run2(c):
p2.clear()
p2.set_xlim([0,15])
p2.set_ylim([0,100])
y = c*x
p2.plot(x,y,'r')
ani1 = animation.FuncAnimation(fig,run1,gen1,interval=1)
ani2 = animation.FuncAnimation(fig,run2,gen2,interval=1)
fig.show()
I tried creating a single subplot instead of p1 and p2 and have both the plots graphed in that single subplot. That is just plotting one graph and not both of them. As far as I can say it is because one of them is getting cleared right after it is plotted.
How do I get around this problem?
As you do not show the code that is actually producing the problem, it's hard to tell where the problem lies.
But to answer the question of how to animate two lines in the same axes (subplot), we can just get rid of the clear() command and update the lines, instead of producing a new plot for every frame (which is more efficient anyways).
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = np.linspace(0,15,100)
fig = plt.figure()
p1 = fig.add_subplot(111)
p1.set_xlim([0,15])
p1.set_ylim([0,100])
# set up empty lines to be updates later on
l1, = p1.plot([],[],'b')
l2, = p1.plot([],[],'r')
def gen1():
i = 0.5
while(True):
yield i
i += 0.1
def gen2():
j = 0
while(True):
yield j
j += 1
def run1(c):
y = c*x
l1.set_data(x,y)
def run2(c):
y = c*x
l2.set_data(x,y)
ani1 = animation.FuncAnimation(fig,run1,gen1,interval=1)
ani2 = animation.FuncAnimation(fig,run2,gen2,interval=1)
plt.show()

Speeding up vectorized eye-tracking algorithm in numpy

I'm trying to implement Fabian Timm's eye-tracking algorithm [http://www.inb.uni-luebeck.de/publikationen/pdfs/TiBa11b.pdf] (found here: [http://thume.ca/projects/2012/11/04/simple-accurate-eye-center-tracking-in-opencv/]) in numpy and OpenCV and I've hit a snag. I think I've vectorized my implementation decently enough, but it's still not fast enough to run in real time and it doesn't detect pupils with as much accuracy as I had hoped. This is my first time using numpy, so I'm not sure what I've done wrong.
def find_pupil(eye):
eye_len = np.arange(eye.shape[0])
xx,yy = np.meshgrid(eye_len,eye_len) #coordinates
XX,YY = np.meshgrid(xx.ravel(),yy.ravel()) #all distance vectors
Dx,Dy = [YY-XX, YY-XX] #y2-y1, x2-x1 -- simpler this way because YY = XXT
Dlen = np.sqrt(Dx**2+Dy**2)
Dx,Dy = [Dx/Dlen, Dy/Dlen] #normalized
Gx,Gy = np.gradient(eye)
Gmagn = np.sqrt(Gx**2+Gy**2)
Gx,Gy = [Gx/Gmagn,Gy/Gmagn] #normalized
GX,GY = np.meshgrid(Gx.ravel(),Gy.ravel())
X = (GX*Dx+GY*Dy)**2
eye = cv2.bitwise_not(cv2.GaussianBlur(eye,(5,5),0.005*eye.shape[1])) #inverting and blurring eye for use as w
eyem = np.repeat(eye.ravel()[np.newaxis,:],eye.size,0)
C = (np.nansum(eyem*X, axis=0)/eye.size).reshape(eye.shape)
return np.unravel_index(C.argmax(), C.shape)
and the rest of the code:
def find_eyes(face):
left_x, left_y = [int(floor(0.5 * face.shape[0])), int(floor(0.2 * face.shape[1]))]
right_x, right_y = [int(floor(0.1 * face.shape[0])), int(floor(0.2 * face.shape[1]))]
area = int(floor(0.2 * face.shape[0]))
left_eye = (left_x, left_y, area, area)
right_eye = (right_x, right_y, area, area)
return [left_eye,right_eye]
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
eyes = find_eyes(roi_gray)
for (ex,ey,ew,eh) in eyes:
eye_gray = roi_gray[ey:ey+eh,ex:ex+ew]
eye_color = roi_color[ey:ey+eh,ex:ex+ew]
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(255,0,0),2)
px,py = find_pupil(eye_gray)
cv2.rectangle(eye_color,(px,py),(px+1,py+1),(255,0,0),2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
You can perform many of those operations that save replicated elements and then perform some mathematical opertaions by directly performing the mathematical operatrions after creating singleton dimensions that would allow NumPy broadcasting. Thus, there would be two benefits - On the fly operations to save workspace memory and performance boost. Also, at the end, we can replace the nansum calculation with a simplified version. Thus, with all of that philosophy in mind, here's one modified approach -
def find_pupil_v2(face, x, y, w, h):
eye = face[x:x+w,y:y+h]
eye_len = np.arange(eye.shape[0])
N = eye_len.size**2
eye_len_diff = eye_len[:,None] - eye_len
Dlen = np.sqrt(2*((eye_len_diff)**2))
Dxy0 = eye_len_diff/Dlen
Gx0,Gy0 = np.gradient(eye)
Gmagn = np.sqrt(Gx0**2+Gy0**2)
Gx,Gy = [Gx0/Gmagn,Gy0/Gmagn] #normalized
B0 = Gy[:,:,None]*Dxy0[:,None,:]
C0 = Gx[:,None,:]*Dxy0
X = ((C0.transpose(1,0,2)[:,None,:,:]+B0[:,:,None,:]).reshape(N,N))**2
eye1 = cv2.bitwise_not(cv2.GaussianBlur(eye,(5,5),0.005*eye.shape[1]))
C = (np.nansum(X,0)*eye1.ravel()/eye1.size).reshape(eye1.shape)
return np.unravel_index(C.argmax(), C.shape)
There's one repeat still left in it at Dxy. It might be possible to avoid that step and Dxy0 could be fed directly into the step that uses Dxy to give us X, but I haven't worked through it. Everything's converted to broadcasting based!
Runtime test and output verification -
In [539]: # Inputs with random elements
...: face = np.random.randint(0,10,(256,256)).astype('uint8')
...: x = 40
...: y = 60
...: w = 64
...: h = 64
...:
In [540]: find_pupil(face,x,y,w,h)
Out[540]: (32, 63)
In [541]: find_pupil_v2(face,x,y,w,h)
Out[541]: (32, 63)
In [542]: %timeit find_pupil(face,x,y,w,h)
1 loops, best of 3: 4.15 s per loop
In [543]: %timeit find_pupil_v2(face,x,y,w,h)
1 loops, best of 3: 529 ms per loop
It seems we are getting close to 8x speedup!

Matplotlib Animation: updating radial view limit for polar plot

I'm trying to create an animated polar plot that, where the radial view limit increases/decreases to accommodate the radius. The yaxis updates just fine if I set polar=False, but it doesn't work correctly for a polar axis.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def data_gen():
t = data_gen.t
cnt = 0
while cnt < 1000:
cnt+=1
t += 0.05
yield t, 1.1 + np.sin(2*np.pi*t) * np.exp(t/10.)
data_gen.t = 0
plt.rc ('grid', color='g', lw=1, ls='-')
plt.rc ('xtick', labelsize=15, color='b')
plt.rc ('ytick', labelsize=15, color='b')
fig = plt.figure(figsize=(8,8))
ax1 = fig.add_axes([.05, .90, .9, .08], polar=False, axisbg='#BFBFBF', xticks=[], yticks=[])
ax2 = fig.add_axes([.05, .05, .9, .8], polar=True, axisbg='k')
#ax = fig.add_axes([.1,.1,.8,.8], polar=False, axisbg='k')
line, = ax2.plot([], [], lw=2)
ax2.set_ylim(0, 2.2)
ax2.set_xlim(0, 140)
ax2.grid(1)
xdata, ydata = [], []
title = ax1.text (0.02, 0.5, '', fontsize=14, transform=ax1.transAxes)
def init():
line.set_data([], [])
title.set_text ('')
return line, title
def run(data):
# update the data
t,y = data
xdata.append(t)
ydata.append(y)
ymin, ymax = ax2.get_ylim()
if y >= ymax:
ax2.set_ylim (ymin, 2*ymax)
ax2.figure.canvas.draw()
title.set_text ("time = %.3f, y(t) = 1.1 + sin(2*pi*t) + exp(t/10) = %.3f" % (t, y))
line.set_data(xdata, ydata)
return line, title
ani = animation.FuncAnimation(fig, run, data_gen, init, blit=True, interval=100, repeat=False)
Actually, the view limit does adjust, but the tick labels stay the same. Inserting raw_input() after the canvas is redrawn reveals that everything is redrawn correctly, but then the tick labels revert back to what they were before. Stranger still, this doesn't occur until the update() function is called a second time and returns.
I didn't have this problem when I animated the plot the old way, by calling draw() repeatedly. But I'd rather do it the right way with the animation module, as the old way had performance problems (The above code block is only to demonstrate the problem, and isn't the actual program that I'm writing).
I should note that I'm still learning MPL, so I apologize if some of my terminology is wrong.
If you use blit, the background is saved in cache for fast draw, you can clear the cache when the axis range is changed, add ani._blit_cache.clear():
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def data_gen():
t = data_gen.t
cnt = 0
while cnt < 1000:
cnt+=1
t += 0.05
yield t, 1.1 + np.sin(2*np.pi*t) * np.exp(t/10.)
data_gen.t = 0
plt.rc ('grid', color='g', lw=1, ls='-')
plt.rc ('xtick', labelsize=15, color='b')
plt.rc ('ytick', labelsize=15, color='b')
fig = plt.figure(figsize=(8,8))
ax1 = fig.add_axes([.05, .90, .9, .08], polar=False, axisbg='#BFBFBF', xticks=[], yticks=[])
ax2 = fig.add_axes([.05, .05, .9, .8], polar=True, axisbg='k')
#ax = fig.add_axes([.1,.1,.8,.8], polar=False, axisbg='k')
line, = ax2.plot([], [], lw=2)
ax2.set_ylim(0, 2.2)
ax2.set_xlim(0, 140)
ax2.grid(1)
xdata, ydata = [], []
title = ax1.text (0.02, 0.5, '', fontsize=14, transform=ax1.transAxes)
def init():
line.set_data([], [])
title.set_text ('')
return line, title
def run(data):
# update the data
t,y = data
xdata.append(t)
ydata.append(y)
ymin, ymax = ax2.get_ylim()
if y >= ymax:
ax2.set_ylim (ymin, 2*ymax)
ani._blit_cache.clear() # <- add to clear background from blit cache
title.set_text('') # <- eliminate text artifact in title
ax2.figure.canvas.draw()
title.set_text ("time = %.3f, y(t) = 1.1 + sin(2*pi*t) + exp(t/10) = %.3f" % (t, y))
line.set_data(xdata, ydata)
return line, title
ani = animation.FuncAnimation(fig, run, data_gen, init, blit=True, interval=100, repeat=False)

Resources