loading...
  1. OpenCV Real-time Graph Plot using Matplotlib or Python-Drawnow

OpenCV Real-time Graph Plot using Matplotlib or Python-Drawnow

Hello Friends,

In this tutorial, we’ll see how we can use Matplotlib to generate live-opencv graphs. We’ll look at atleast three ways of doing this with their relevant source code.

Method #1 : A simple one
Here’s the first working version of the code (requires at least version Matplotlib 1.1.0 from 2011-11-14):

1
2
3
4
5
6
7
8
9
10
11
12
13
import numpy as np
import matplotlib.pyplot as plt
 
plt.axis([0, 10, 0, 1])
plt.ion()
 
for i in range(10):
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.05)
 
while True:
    plt.pause(0.05)

Note some of the changes:

Call plt.ion() in order to enable interactive plotting. plt.show(block=False) is no longer available.
Call plt.pause(0.05) to both draw the new data and it runs the GUI’s event loop (allowing for mouse interaction).
The while loop at the end is to keep the window up after all data is plotted.

Method #2: Real-time plot with Matplotlib Animation API
If you’re interested in realtime plotting, I’d recommend looking into matplotlib’s animation API.
In particular, using blit to avoid redrawing the background on every frame can give you substantial speed gains (~10x):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
 
import numpy as np
import time
import matplotlib
matplotlib.use('GTKAgg')
from matplotlib import pyplot as plt
 
 
def randomwalk(dims=(256, 256), n=20, sigma=5, alpha=0.95, seed=1):
    """ A simple random walk with memory """
 
    r, c = dims
    gen = np.random.RandomState(seed)
    pos = gen.rand(2, n) * ((r,), (c,))
    old_delta = gen.randn(2, n) * sigma
 
    while True:
        delta = (1. - alpha) * gen.randn(2, n) * sigma + alpha * old_delta
        pos += delta
        for ii in xrange(n):
            if not (0. <= pos[0, ii] < r):
                pos[0, ii] = abs(pos[0, ii] % r)
            if not (0. <= pos[1, ii] < c):
                pos[1, ii] = abs(pos[1, ii] % c)
        old_delta = delta
        yield pos
 
 
def run(niter=1000, doblit=True):
    """
    Display the simulation using matplotlib, optionally using blit for speed
    """
 
    fig, ax = plt.subplots(1, 1)
    ax.set_aspect('equal')
    ax.set_xlim(0, 255)
    ax.set_ylim(0, 255)
    ax.hold(True)
    rw = randomwalk()
    x, y = rw.next()
 
    plt.show(False)
    plt.draw()
 
    if doblit:
        # cache the background
        background = fig.canvas.copy_from_bbox(ax.bbox)
 
    points = ax.plot(x, y, 'o')[0]
    tic = time.time()
 
    for ii in xrange(niter):
 
        # update the xy data
        x, y = rw.next()
        points.set_data(x, y)
 
        if doblit:
            # restore background
            fig.canvas.restore_region(background)
 
            # redraw just the points
            ax.draw_artist(points)
 
            # fill in the axes rectangle
            fig.canvas.blit(ax.bbox)
 
        else:
            # redraw everything
            fig.canvas.draw()
 
    plt.close(fig)
    print "Blit = %s, average FPS: %.2f" % (
        str(doblit), niter / (time.time() - tic))
 
if __name__ == '__main__':
    run(doblit=False)
    run(doblit=True)

Output:

Blit = False, average FPS: 54.37
Blit = True, average FPS: 438.27

Method#3 : Without using MatplotLib
There is a package available called drawnow on GitHub as “python-drawnow”.
This provides an interface similar to MATLAB’s drawnow which allows you to easily update a figure.
python-drawnow is a thin wrapper around plt.draw but provides the ability to confirm (or debug) after figure display.

An example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import matplotlib.pyplot as plt
from drawnow import drawnow
 
def make_fig():
    plt.scatter(x, y)  # I think you meant this
 
plt.ion()  # enable interactivity
fig = plt.figure()  # make a figure
 
x = list()
y = list()
 
for i in range(1000):
    temp_y = np.random.random()
    x.append(i)
    y.append(temp_y)  # or any arbitrary update to your figure's data
    i += 1
    drawnow(make_fig)
How to detect a Christmas Tree using Python?




Leave a Reply

Your email address will not be published. Required fields are marked *

Welcome to OpenCV World !! Come as a Guest, stay as a Family