From dc1af8946ee89455367e0b90884226eb7ee5fe7e Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Sun, 9 Feb 2014 09:42:04 -0500 Subject: [PATCH] Added a few new examples --- examples/BarGraphItem.py | 41 +++++++++++ examples/CustomGraphItem.py | 136 ++++++++++++++++++++++++++++++++++++ examples/FillBetweenItem.py | 50 +++++++++++++ examples/ViewBoxFeatures.py | 90 ++++++++++++++++++++++++ 4 files changed, 317 insertions(+) create mode 100644 examples/BarGraphItem.py create mode 100644 examples/CustomGraphItem.py create mode 100644 examples/FillBetweenItem.py create mode 100644 examples/ViewBoxFeatures.py diff --git a/examples/BarGraphItem.py b/examples/BarGraphItem.py new file mode 100644 index 00000000..c93a1966 --- /dev/null +++ b/examples/BarGraphItem.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Simple example using BarGraphItem +""" +import initExample ## Add path to library (just for examples; you do not need this) + +import pyqtgraph as pg +from pyqtgraph.Qt import QtCore, QtGui +import numpy as np + +win = pg.plot() +win.setWindowTitle('pyqtgraph example: BarGraphItem') + +x = np.arange(10) +y1 = np.sin(x) +y2 = 1.1 * np.sin(x+1) +y3 = 1.2 * np.sin(x+2) + +bg1 = pg.BarGraphItem(x=x, height=y1, width=0.3, brush='r') +bg2 = pg.BarGraphItem(x=x+0.33, height=y2, width=0.3, brush='g') +bg3 = pg.BarGraphItem(x=x+0.66, height=y3, width=0.3, brush='b') + +win.addItem(bg1) +win.addItem(bg2) +win.addItem(bg3) + + +# Final example shows how to handle mouse clicks: +class BarGraph(pg.BarGraphItem): + def mouseClickEvent(self, event): + print "clicked" + + +bg = BarGraph(x=x, y=y1*0.3+2, height=0.4+y1*0.2, width=0.8) +win.addItem(bg) + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_() diff --git a/examples/CustomGraphItem.py b/examples/CustomGraphItem.py new file mode 100644 index 00000000..695768e2 --- /dev/null +++ b/examples/CustomGraphItem.py @@ -0,0 +1,136 @@ +# -*- coding: utf-8 -*- +""" +Simple example of subclassing GraphItem. +""" + +import initExample ## Add path to library (just for examples; you do not need this) + +import pyqtgraph as pg +from pyqtgraph.Qt import QtCore, QtGui +import numpy as np + +# Enable antialiasing for prettier plots +pg.setConfigOptions(antialias=True) + +w = pg.GraphicsWindow() +w.setWindowTitle('pyqtgraph example: CustomGraphItem') +v = w.addViewBox() +v.setAspectLocked() + +class Graph(pg.GraphItem): + def __init__(self): + self.dragPoint = None + self.dragOffset = None + self.textItems = [] + pg.GraphItem.__init__(self) + self.scatter.sigClicked.connect(self.clicked) + + def setData(self, **kwds): + self.text = kwds.pop('text', []) + self.data = kwds + if 'pos' in self.data: + npts = self.data['pos'].shape[0] + self.data['data'] = np.empty(npts, dtype=[('index', int)]) + self.data['data']['index'] = np.arange(npts) + self.setTexts(self.text) + self.updateGraph() + + def setTexts(self, text): + for i in self.textItems: + i.scene().removeItem(i) + self.textItems = [] + for t in text: + item = pg.TextItem(t) + self.textItems.append(item) + item.setParentItem(self) + + def updateGraph(self): + pg.GraphItem.setData(self, **self.data) + for i,item in enumerate(self.textItems): + item.setPos(*self.data['pos'][i]) + + + def mouseDragEvent(self, ev): + if ev.button() != QtCore.Qt.LeftButton: + ev.ignore() + return + + if ev.isStart(): + # We are already one step into the drag. + # Find the point(s) at the mouse cursor when the button was first + # pressed: + pos = ev.buttonDownPos() + pts = self.scatter.pointsAt(pos) + if len(pts) == 0: + ev.ignore() + return + self.dragPoint = pts[0] + ind = pts[0].data()[0] + self.dragOffset = self.data['pos'][ind] - pos + elif ev.isFinish(): + self.dragPoint = None + return + else: + if self.dragPoint is None: + ev.ignore() + return + + ind = self.dragPoint.data()[0] + self.data['pos'][ind] = ev.pos() + self.dragOffset + self.updateGraph() + ev.accept() + + def clicked(self, pts): + print("clicked: %s" % pts) + + +g = Graph() +v.addItem(g) + +## Define positions of nodes +pos = np.array([ + [0,0], + [10,0], + [0,10], + [10,10], + [5,5], + [15,5] + ], dtype=float) + +## Define the set of connections in the graph +adj = np.array([ + [0,1], + [1,3], + [3,2], + [2,0], + [1,5], + [3,5], + ]) + +## Define the symbol to use for each node (this is optional) +symbols = ['o','o','o','o','t','+'] + +## Define the line style for each connection (this is optional) +lines = np.array([ + (255,0,0,255,1), + (255,0,255,255,2), + (255,0,255,255,3), + (255,255,0,255,2), + (255,0,0,255,1), + (255,255,255,255,4), + ], dtype=[('red',np.ubyte),('green',np.ubyte),('blue',np.ubyte),('alpha',np.ubyte),('width',float)]) + +## Define text to show next to each symbol +texts = ["Point %d" % i for i in range(6)] + +## Update the graph +g.setData(pos=pos, adj=adj, pen=lines, size=1, symbol=symbols, pxMode=False, text=texts) + + + + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_() diff --git a/examples/FillBetweenItem.py b/examples/FillBetweenItem.py new file mode 100644 index 00000000..74dd89bc --- /dev/null +++ b/examples/FillBetweenItem.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +""" +Demonstrates use of FillBetweenItem to fill the space between two plot curves. +""" +import initExample ## Add path to library (just for examples; you do not need this) + +import pyqtgraph as pg +from pyqtgraph.Qt import QtGui, QtCore +import numpy as np + +win = pg.plot() +win.setWindowTitle('pyqtgraph example: FillBetweenItem') +win.setXRange(-10, 10) +win.setYRange(-10, 10) + +N = 200 +x = np.linspace(-10, 10, N) +gauss = np.exp(-x**2 / 20.) +mn = mx = np.zeros(len(x)) +curves = [win.plot(x=x, y=np.zeros(len(x)), pen='k') for i in range(4)] +brushes = [0.5, (100, 100, 255), 0.5] +fills = [pg.FillBetweenItem(curves[i], curves[i+1], brushes[i]) for i in range(3)] +for f in fills: + win.addItem(f) + +def update(): + global mx, mn, curves, gauss, x + a = 5 / abs(np.random.normal(loc=1, scale=0.2)) + y1 = -np.abs(a*gauss + np.random.normal(size=len(x))) + y2 = np.abs(a*gauss + np.random.normal(size=len(x))) + + s = 0.01 + mn = np.where(y1mx, y2, mx) * (1-s) + y2 * s + curves[0].setData(x, mn) + curves[1].setData(x, y1) + curves[2].setData(x, y2) + curves[3].setData(x, mx) + + +timer = QtCore.QTimer() +timer.timeout.connect(update) +timer.start(30) + + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_() diff --git a/examples/ViewBoxFeatures.py b/examples/ViewBoxFeatures.py new file mode 100644 index 00000000..6388e41b --- /dev/null +++ b/examples/ViewBoxFeatures.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- +""" +ViewBox is the general-purpose graphical container that allows the user to +zoom / pan to inspect any area of a 2D coordinate system. + +This example demonstrates many of the features ViewBox provides. +""" + +import initExample ## Add path to library (just for examples; you do not need this) + +import pyqtgraph as pg +from pyqtgraph.Qt import QtCore, QtGui +import numpy as np + +x = np.arange(1000, dtype=float) +y = np.random.normal(size=1000) +y += 5 * np.sin(x/100) + +win = pg.GraphicsWindow() +win.setWindowTitle('pyqtgraph example: ____') +win.resize(1000, 800) +win.ci.setBorder((50, 50, 100)) + +sub1 = win.addLayout() +sub1.addLabel("Standard mouse interaction:
left-drag to pan, right-drag to zoom.") +sub1.nextRow() +v1 = sub1.addViewBox() +l1 = pg.PlotDataItem(y) +v1.addItem(l1) + + +sub2 = win.addLayout() +sub2.addLabel("One-button mouse interaction:
left-drag zoom to box, wheel to zoom out.") +sub2.nextRow() +v2 = sub2.addViewBox() +v2.setMouseMode(v2.RectMode) +l2 = pg.PlotDataItem(y) +v2.addItem(l2) + +win.nextRow() + +sub3 = win.addLayout() +sub3.addLabel("Locked aspect ratio when zooming.") +sub3.nextRow() +v3 = sub3.addViewBox() +v3.setAspectLocked(1.0) +l3 = pg.PlotDataItem(y) +v3.addItem(l3) + +sub4 = win.addLayout() +sub4.addLabel("View limits:
prevent panning or zooming past limits.") +sub4.nextRow() +v4 = sub4.addViewBox() +v4.setLimits(xMin=-100, xMax=1100, + minXRange=20, maxXRange=500, + yMin=-10, yMax=10, + minYRange=1, maxYRange=10) +l4 = pg.PlotDataItem(y) +v4.addItem(l4) + +win.nextRow() + +sub5 = win.addLayout() +sub5.addLabel("Linked axes: Data in this plot is always X-aligned to
the plot above.") +sub5.nextRow() +v5 = sub5.addViewBox() +v5.setXLink(v3) +l5 = pg.PlotDataItem(y) +v5.addItem(l5) + +sub6 = win.addLayout() +sub6.addLabel("Disable mouse: Per-axis control over mouse input.
" + "Auto-scale-visible: Automatically fit *visible* data within view
" + "(try panning left-right).") +sub6.nextRow() +v6 = sub6.addViewBox() +v6.setMouseEnabled(x=True, y=False) +v6.enableAutoRange(x=False, y=True) +v6.setXRange(300, 450) +v6.setAutoVisible(x=False, y=True) +l6 = pg.PlotDataItem(y) +v6.addItem(l6) + + + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + QtGui.QApplication.instance().exec_()