File: multiplePlotSpeedTest.py

package info (click to toggle)
python-pyqtgraph 0.13.7-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 8,068 kB
  • sloc: python: 54,043; makefile: 129; ansic: 40; sh: 2
file content (79 lines) | stat: -rw-r--r-- 2,053 bytes parent folder | download | duplicates (2)
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
from time import perf_counter

import numpy as np

import pyqtgraph as pg

app = pg.mkQApp()
plt = pg.PlotWidget()

app.processEvents()

## Putting this at the beginning or end does not have much effect
plt.show()   

## The auto-range is recomputed after each item is added,
## so disabling it before plotting helps
plt.enableAutoRange(False, False)

def plot():
    start = perf_counter()
    n = 15
    pts = 100
    x = np.linspace(0, 0.8, pts)
    y = np.random.random(size=pts)*0.8
    for i in range(n):
        for j in range(n):
            ## calling PlotWidget.plot() generates a PlotDataItem, which 
            ## has a bit more overhead than PlotCurveItem, which is all 
            ## we need here. This overhead adds up quickly and makes a big
            ## difference in speed.
            
            plt.addItem(pg.PlotCurveItem(x=x+i, y=y+j))
            
    dt = perf_counter() - start
    print(f"Create plots took: {dt * 1000:.3f} ms")

## Plot and clear 5 times, printing the time it took
for _ in range(5):
    plt.clear()
    plot()
    app.processEvents()
    plt.autoRange()





def fastPlot():
    ## Different approach:  generate a single item with all data points.
    ## This runs many times faster.
    start = perf_counter()
    n = 15
    pts = 100
    x = np.linspace(0, 0.8, pts)
    y = np.random.random(size=pts)*0.8
    shape = (n, n, pts)
    xdata = np.empty(shape)
    xdata[:] = x + np.arange(shape[1]).reshape((1,-1,1))
    ydata = np.empty(shape)
    ydata[:] = y + np.arange(shape[0]).reshape((-1,1,1))
    conn = np.ones(shape, dtype=bool)
    conn[...,-1] = False # make sure plots are disconnected
    item = pg.PlotCurveItem()
    item.setData(xdata.ravel(), ydata.ravel(), connect=conn.ravel())
    plt.addItem(item)
    
    dt = perf_counter() - start
    print("Create plots took: %0.3fms" % (dt*1000))


## Plot and clear 5 times, printing the time it took
for _ in range(5):
    plt.clear()
    fastPlot()
    app.processEvents()
    plt.autoRange()

if __name__ == '__main__':
    pg.exec()