From 63c3b36a03bb91b3186b34584bbb2785aa015cac Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 13 Feb 2013 13:00:50 -0500 Subject: [PATCH] added a few examples --- examples/LogPlotTest.py | 42 ++++++++++++++ examples/SimplePlot.py | 12 ++++ examples/multiplePlotSpeedTest.py | 93 +++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 examples/LogPlotTest.py create mode 100644 examples/SimplePlot.py create mode 100644 examples/multiplePlotSpeedTest.py diff --git a/examples/LogPlotTest.py b/examples/LogPlotTest.py new file mode 100644 index 00000000..a5b07520 --- /dev/null +++ b/examples/LogPlotTest.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- + +## This example demonstrates many of the 2D plotting capabilities +## in pyqtgraph. All of the plots may be panned/scaled by dragging with +## the left/right mouse buttons. Right click on any plot to show a context menu. + + +import initExample ## Add path to library (just for examples; you do not need this) + + +from pyqtgraph.Qt import QtGui, QtCore +import numpy as np +import pyqtgraph as pg + +#QtGui.QApplication.setGraphicsSystem('raster') +app = QtGui.QApplication([]) +#mw = QtGui.QMainWindow() +#mw.resize(800,800) + +win = pg.GraphicsWindow(title="Basic plotting examples") +win.resize(1000,600) + + + +p5 = win.addPlot(title="Scatter plot, axis labels, log scale") +x = np.random.normal(size=1000) * 1e-5 +y = x*1000 + 0.005 * np.random.normal(size=1000) +y -= y.min()-1.0 +mask = x > 1e-15 +x = x[mask] +y = y[mask] +p5.plot(x, y, pen=None, symbol='t', symbolPen=None, symbolSize=10, symbolBrush=(100, 100, 255, 50)) +p5.setLabel('left', "Y Axis", units='A') +p5.setLabel('bottom', "Y Axis", units='s') +p5.setLogMode(x=True, y=False) + + +## 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/SimplePlot.py b/examples/SimplePlot.py new file mode 100644 index 00000000..ec40cf16 --- /dev/null +++ b/examples/SimplePlot.py @@ -0,0 +1,12 @@ +import initExample ## Add path to library (just for examples; you do not need this) + +from pyqtgraph.Qt import QtGui, QtCore +import pyqtgraph as pg +import numpy as np +pg.plot(np.random.normal(size=100000), title="Simplest possible plotting example") + +## 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'): + pg.QtGui.QApplication.exec_() diff --git a/examples/multiplePlotSpeedTest.py b/examples/multiplePlotSpeedTest.py new file mode 100644 index 00000000..bc54bb51 --- /dev/null +++ b/examples/multiplePlotSpeedTest.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- +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 + +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 = pg.ptime.time() + n = 15 + pts = 100 + x = np.linspace(0, 0.8, pts) + y = np.random.random(size=pts)*0.8 + for i in xrange(n): + for j in xrange(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.plot(x=x+i, y=y+j) + plt.addItem(pg.PlotCurveItem(x=x+i, y=y+j)) + + #path = pg.arrayToQPath(x+i, y+j) + #item = QtGui.QGraphicsPathItem(path) + #item.setPen(pg.mkPen('w')) + #plt.addItem(item) + + dt = pg.ptime.time() - start + print "Create plots took: %0.3fms" % (dt*1000) + +## Plot and clear 5 times, printing the time it took +for i in range(5): + plt.clear() + plot() + app.processEvents() + plt.autoRange() + + + + + +def fastPlot(): + ## Different approach: generate a single item with all data points. + ## This runs about 20x faster. + start = pg.ptime.time() + n = 15 + pts = 100 + x = np.linspace(0, 0.8, pts) + y = np.random.random(size=pts)*0.8 + xdata = np.empty((n, n, pts)) + xdata[:] = x.reshape(1,1,pts) + np.arange(n).reshape(n,1,1) + ydata = np.empty((n, n, pts)) + ydata[:] = y.reshape(1,1,pts) + np.arange(n).reshape(1,n,1) + conn = np.ones((n*n,pts)) + conn[:,-1] = False # make sure plots are disconnected + path = pg.arrayToQPath(xdata.flatten(), ydata.flatten(), conn.flatten()) + item = QtGui.QGraphicsPathItem(path) + item.setPen(pg.mkPen('w')) + plt.addItem(item) + + dt = pg.ptime.time() - start + print "Create plots took: %0.3fms" % (dt*1000) + + +## Plot and clear 5 times, printing the time it took +if hasattr(pg, 'arrayToQPath'): + for i in range(5): + plt.clear() + fastPlot() + app.processEvents() +else: + print "Skipping fast tests--arrayToQPath function is missing." + +plt.autoRange() + +## 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_()