1# -*- coding: utf-8 -*-
2"""
3Demonstrates very basic use of PColorMeshItem
4"""
5
6## Add path to library (just for examples; you do not need this)
7import initExample
8
9from pyqtgraph.Qt import QtCore
10import numpy as np
11import pyqtgraph as pg
12
13app = pg.mkQApp("PColorMesh Example")
14
15## Create window with GraphicsView widget
16win = pg.GraphicsLayoutWidget()
17win.show()  ## show widget alone in its own window
18win.setWindowTitle('pyqtgraph example: pColorMeshItem')
19view = win.addViewBox()
20
21
22## Create data
23
24# To enhance the non-grid meshing, we randomize the polygon vertices per and
25# certain amount
26randomness = 5
27
28# x and y being the vertices of the polygons, they share the same shape
29# However the shape can be different in both dimension
30xn = 50 # nb points along x
31yn = 40 # nb points along y
32
33
34x = np.repeat(np.arange(1, xn+1), yn).reshape(xn, yn)\
35    + np.random.random((xn, yn))*randomness
36y = np.tile(np.arange(1, yn+1), xn).reshape(xn, yn)\
37    + np.random.random((xn, yn))*randomness
38x.sort(axis=0)
39y.sort(axis=0)
40
41
42# z being the color of the polygons its shape must be decreased by one in each dimension
43z = np.exp(-(x*xn)**2/1000)[:-1,:-1]
44
45## Create image item
46edgecolors   = None
47antialiasing = False
48# edgecolors = {'color':'w', 'width':2} # May be uncommened to see edgecolor effect
49# antialiasing = True # May be uncommened to see antialiasing effect
50pcmi = pg.PColorMeshItem(edgecolors=edgecolors, antialiasing=antialiasing)
51view.addItem(pcmi)
52
53
54## Set the animation
55fps = 25 # Frame per second of the animation
56
57# Wave parameters
58wave_amplitude  = 3
59wave_speed      = 0.3
60wave_length     = 10
61color_speed     = 0.3
62
63timer = QtCore.QTimer()
64timer.setSingleShot(True)
65# not using QTimer.singleShot() because of persistence on PyQt. see PR #1605
66
67i=0
68def updateData():
69    global i
70
71    ## Display the new data set
72    new_x = x
73    new_y = y+wave_amplitude*np.cos(x/wave_length+i)
74    new_z = np.exp(-(x-np.cos(i*color_speed)*xn)**2/1000)[:-1,:-1]
75    pcmi.setData(new_x,
76                 new_y,
77                 new_z)
78
79    i += wave_speed
80    timer.start(1000//fps)
81
82timer.timeout.connect(updateData)
83updateData()
84
85if __name__ == '__main__':
86    pg.exec()
87