1# -*- coding: utf-8 -*-
2"""
3This example demonstrates many of the 2D plotting capabilities
4in pyqtgraph. All of the plots may be panned/scaled by dragging with
5the left/right mouse buttons. Right click on any plot to show a context menu.
6"""
7
8import initExample ## Add path to library (just for examples; you do not need this)
9
10
11from pyqtgraph.Qt import QtGui, QtCore
12import numpy as np
13import pyqtgraph as pg
14
15app = pg.mkQApp("Plotting Example")
16#mw = QtGui.QMainWindow()
17#mw.resize(800,800)
18
19win = pg.GraphicsLayoutWidget(show=True, title="Basic plotting examples")
20win.resize(1000,600)
21win.setWindowTitle('pyqtgraph example: Plotting')
22
23# Enable antialiasing for prettier plots
24pg.setConfigOptions(antialias=True)
25
26p1 = win.addPlot(title="Basic array plotting", y=np.random.normal(size=100))
27
28p2 = win.addPlot(title="Multiple curves")
29p2.plot(np.random.normal(size=100), pen=(255,0,0), name="Red curve")
30p2.plot(np.random.normal(size=110)+5, pen=(0,255,0), name="Green curve")
31p2.plot(np.random.normal(size=120)+10, pen=(0,0,255), name="Blue curve")
32
33p3 = win.addPlot(title="Drawing with points")
34p3.plot(np.random.normal(size=100), pen=(200,200,200), symbolBrush=(255,0,0), symbolPen='w')
35
36
37win.nextRow()
38
39p4 = win.addPlot(title="Parametric, grid enabled")
40x = np.cos(np.linspace(0, 2*np.pi, 1000))
41y = np.sin(np.linspace(0, 4*np.pi, 1000))
42p4.plot(x, y)
43p4.showGrid(x=True, y=True)
44
45p5 = win.addPlot(title="Scatter plot, axis labels, log scale")
46x = np.random.normal(size=1000) * 1e-5
47y = x*1000 + 0.005 * np.random.normal(size=1000)
48y -= y.min()-1.0
49mask = x > 1e-15
50x = x[mask]
51y = y[mask]
52p5.plot(x, y, pen=None, symbol='t', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 50))
53p5.setLabel('left', "Y Axis", units='A')
54p5.setLabel('bottom', "Y Axis", units='s')
55p5.setLogMode(x=True, y=False)
56
57p6 = win.addPlot(title="Updating plot")
58curve = p6.plot(pen='y')
59data = np.random.normal(size=(10,1000))
60ptr = 0
61def update():
62    global curve, data, ptr, p6
63    curve.setData(data[ptr%10])
64    if ptr == 0:
65        p6.enableAutoRange('xy', False)  ## stop auto-scaling after the first data set is plotted
66    ptr += 1
67timer = QtCore.QTimer()
68timer.timeout.connect(update)
69timer.start(50)
70
71
72win.nextRow()
73
74p7 = win.addPlot(title="Filled plot, axis disabled")
75y = np.sin(np.linspace(0, 10, 1000)) + np.random.normal(size=1000, scale=0.1)
76p7.plot(y, fillLevel=-0.3, brush=(50,50,200,100))
77p7.showAxis('bottom', False)
78
79
80x2 = np.linspace(-100, 100, 1000)
81data2 = np.sin(x2) / x2
82p8 = win.addPlot(title="Region Selection")
83p8.plot(data2, pen=(255,255,255,200))
84lr = pg.LinearRegionItem([400,700])
85lr.setZValue(-10)
86p8.addItem(lr)
87
88p9 = win.addPlot(title="Zoom on selected region")
89p9.plot(data2)
90def updatePlot():
91    p9.setXRange(*lr.getRegion(), padding=0)
92def updateRegion():
93    lr.setRegion(p9.getViewBox().viewRange()[0])
94lr.sigRegionChanged.connect(updatePlot)
95p9.sigXRangeChanged.connect(updateRegion)
96updatePlot()
97
98if __name__ == '__main__':
99    pg.exec()
100