├── run_all_plots.sh ├── plot_visvis.py ├── plot_pyqtgraph.py ├── hist_mpl.py ├── plot_mpl.py └── plot_cagraph.py /run_all_plots.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for plot in plot*.py; do 4 | python $plot & 5 | done 6 | -------------------------------------------------------------------------------- /plot_visvis.py: -------------------------------------------------------------------------------- 1 | 2 | import visvis as vv 3 | 4 | import collections 5 | import random 6 | import time 7 | import math 8 | import numpy as np 9 | 10 | class DynamicPlotter(): 11 | 12 | def __init__(self, sampleinterval=0.1, timewindow=10., size=(600,350)): 13 | # Data stuff 14 | self._interval = int(sampleinterval*1000) 15 | self._bufsize = int(timewindow/sampleinterval) 16 | self.databuffer = collections.deque([0.0]*self._bufsize, self._bufsize) 17 | self.x = np.linspace(-timewindow, 0.0, self._bufsize) 18 | self.y = np.zeros(self._bufsize, dtype=np.float) 19 | # Visvis stuff 20 | self.app = vv.use('qt4') 21 | vv.title('Dynamic Plotting with VisVis') 22 | self.line = vv.plot(self.x, self.y, lc='b', lw=3, ms='+') 23 | vv.xlabel('time') 24 | vv.ylabel('amplitude') 25 | self.ax = vv.gca() 26 | 27 | self.timer = vv.Timer(self.app, 50, oneshot=False) 28 | self.timer.Bind(self.updateplot) 29 | self.timer.Start() 30 | 31 | def getdata(self): 32 | frequency = 0.5 33 | noise = random.normalvariate(0., 1.) 34 | new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise 35 | return new 36 | 37 | def updateplot(self, timer): 38 | self.databuffer.append( self.getdata() ) 39 | self.y[:] = self.databuffer 40 | self.line.SetYdata(self.y) 41 | self.ax.SetLimits() 42 | return True 43 | 44 | def run(self): 45 | self.app.Run() 46 | 47 | if __name__ == '__main__': 48 | 49 | m = DynamicPlotter(sampleinterval=0.05, timewindow=10.) 50 | m.run() 51 | 52 | -------------------------------------------------------------------------------- /plot_pyqtgraph.py: -------------------------------------------------------------------------------- 1 | 2 | from pyqtgraph.Qt import QtGui, QtCore 3 | import pyqtgraph as pg 4 | 5 | import collections 6 | import random 7 | import time 8 | import math 9 | import numpy as np 10 | 11 | class DynamicPlotter(): 12 | 13 | def __init__(self, sampleinterval=0.1, timewindow=10., size=(600,350)): 14 | # Data stuff 15 | self._interval = int(sampleinterval*1000) 16 | self._bufsize = int(timewindow/sampleinterval) 17 | self.databuffer = collections.deque([0.0]*self._bufsize, self._bufsize) 18 | self.x = np.linspace(-timewindow, 0.0, self._bufsize) 19 | self.y = np.zeros(self._bufsize, dtype=np.float) 20 | # PyQtGraph stuff 21 | self.app = QtGui.QApplication([]) 22 | self.plt = pg.plot(title='Dynamic Plotting with PyQtGraph') 23 | self.plt.resize(*size) 24 | self.plt.showGrid(x=True, y=True) 25 | self.plt.setLabel('left', 'amplitude', 'V') 26 | self.plt.setLabel('bottom', 'time', 's') 27 | self.curve = self.plt.plot(self.x, self.y, pen=(255,0,0)) 28 | # QTimer 29 | self.timer = QtCore.QTimer() 30 | self.timer.timeout.connect(self.updateplot) 31 | self.timer.start(self._interval) 32 | 33 | def getdata(self): 34 | frequency = 0.5 35 | noise = random.normalvariate(0., 1.) 36 | new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise 37 | return new 38 | 39 | def updateplot(self): 40 | self.databuffer.append( self.getdata() ) 41 | self.y[:] = self.databuffer 42 | self.curve.setData(self.x, self.y) 43 | self.app.processEvents() 44 | 45 | def run(self): 46 | self.app.exec_() 47 | 48 | if __name__ == '__main__': 49 | 50 | m = DynamicPlotter(sampleinterval=0.05, timewindow=10.) 51 | m.run() 52 | 53 | -------------------------------------------------------------------------------- /hist_mpl.py: -------------------------------------------------------------------------------- 1 | 2 | from gi.repository import Gtk, GLib, Gdk, GObject 3 | import collections 4 | import random 5 | import time 6 | 7 | class mpl: 8 | from matplotlib.figure import Figure 9 | from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas 10 | 11 | import numpy as np 12 | 13 | 14 | class Mplotter_nonthreaded(Gtk.Window): 15 | 16 | def __init__(self, interval=100, size=(400,300)): 17 | Gtk.Window.__init__(self) 18 | self.connect("destroy", lambda x : Gtk.main_quit()) 19 | self.set_default_size(*size) 20 | self._interval = int(interval) 21 | self.d = collections.deque([0.0]*1000, 1000) 22 | self.a = np.zeros((1000,)) 23 | self._bins = 30 24 | self._range = (0.0, 15.0) 25 | 26 | self.figure = mpl.Figure() 27 | self.subplt = self.figure.add_subplot(1, 1, 1) 28 | self.canvas = mpl.FigureCanvas(self.figure) 29 | self.add(self.canvas) 30 | 31 | self.set_keep_above(True) 32 | self.show_all() 33 | 34 | n, self.bins, self.patches = self.subplt.hist(self.a, 15, normed=True, 35 | range=self._range) 36 | 37 | def update(self, t=[0.0]): 38 | self.d.append(random.lognormvariate(2.,0.2)) # almost nothing 39 | self.a[:] = self.d 40 | X, _ = np.histogram(self.a, bins=self.bins, normed=True) 41 | 42 | for rect, h in zip(self.patches, X): 43 | rect.set_height(h) 44 | self.subplt.autoscale(True) 45 | 46 | self.canvas.draw() # 20ms 47 | return True 48 | 49 | def run(self): 50 | GLib.timeout_add(self._interval, self.update) 51 | Gtk.main() 52 | 53 | if __name__ == '__main__': 54 | 55 | m = Mplotter_nonthreaded(10) 56 | m.run() 57 | 58 | -------------------------------------------------------------------------------- /plot_mpl.py: -------------------------------------------------------------------------------- 1 | 2 | from gi.repository import Gtk, GLib 3 | 4 | import collections 5 | import random 6 | import time 7 | import math 8 | 9 | class mpl: 10 | from matplotlib.figure import Figure 11 | from matplotlib.backends.backend_gtk3agg import FigureCanvasGTK3Agg as FigureCanvas 12 | 13 | class DynamicPlotter(Gtk.Window): 14 | 15 | def __init__(self, sampleinterval=0.1, timewindow=10., size=(600,350)): 16 | # Gtk stuff 17 | Gtk.Window.__init__(self, title='Dynamic Plotting with Matplotlib + Gtk3') 18 | self.connect("destroy", lambda x : Gtk.main_quit()) 19 | self.set_default_size(*size) 20 | # Data stuff 21 | self._interval = int(sampleinterval*1000) 22 | self._bufsize = int(timewindow/sampleinterval) 23 | self.databuffer = collections.deque([0.0]*self._bufsize, self._bufsize) 24 | self.x = [sampleinterval*i for i in range(-self._bufsize+1,1)] 25 | # MPL stuff 26 | self.figure = mpl.Figure() 27 | self.ax = self.figure.add_subplot(1, 1, 1) 28 | self.ax.grid(True) 29 | self.canvas = mpl.FigureCanvas(self.figure) 30 | self.line, = self.ax.plot(self.x, self.databuffer) 31 | # Gtk stuff 32 | self.add(self.canvas) 33 | self.canvas.show() 34 | self.show_all() 35 | 36 | def getdata(self): 37 | frequency = 0.5 38 | noise = random.normalvariate(0., 1.) 39 | new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise 40 | return new 41 | 42 | def updateplot(self): 43 | self.databuffer.append( self.getdata() ) 44 | self.line.set_ydata(self.databuffer) 45 | self.ax.relim() 46 | self.ax.autoscale_view(False, False, True) 47 | self.canvas.draw() 48 | return True 49 | 50 | def run(self): 51 | GLib.timeout_add(self._interval, self.updateplot ) 52 | Gtk.main() 53 | 54 | if __name__ == '__main__': 55 | 56 | m = DynamicPlotter(sampleinterval=0.05, timewindow=10.) 57 | m.run() 58 | 59 | -------------------------------------------------------------------------------- /plot_cagraph.py: -------------------------------------------------------------------------------- 1 | 2 | from gi.repository import Gtk, GLib, Gdk 3 | 4 | import collections 5 | import random 6 | import time 7 | import math 8 | 9 | class Ca: 10 | # requires GTK3 port of cagraph from 11 | # > https://github.com/ap--/cagraph-1.2-gtk3.git 12 | from cagraph.ca_graph import CaGraph as Graph 13 | from cagraph.ca_graph_file import CaGraphFile as GraphFile 14 | from cagraph.axis.xaxis import CaGraphXAxis as GraphXAxis 15 | from cagraph.axis.yaxis import CaGraphYAxis as GraphYAxis 16 | from cagraph.axis.taxis import CaGraphTAxis as GraphTAxis 17 | from cagraph.ca_graph_grid import CaGraphGrid as GraphGrid 18 | from cagraph.series.line import CaGraphSeriesLine as GraphSeriesLine 19 | from cagraph.series.bar import CaGraphSeriesBar as GraphSeriesBar 20 | from cagraph.series.area import CaGraphSeriesArea as GraphSeriesArea 21 | 22 | 23 | class DynamicPlotter(Gtk.Window): 24 | 25 | def __init__(self, sampleinterval=0.1, timewindow=10., size=(600,350)): 26 | # Gtk stuff 27 | Gtk.Window.__init__(self, title='Dynamic Plotting with CaGraph-1.2-gtk3') 28 | self.connect("destroy", lambda x : Gtk.main_quit()) 29 | self.set_default_size(*size) 30 | # Data stuff 31 | self._interval = int(sampleinterval*1000) 32 | self._bufsize = int(timewindow/sampleinterval) 33 | self.databuffer = collections.deque([0.0]*self._bufsize, self._bufsize) 34 | self.x = [sampleinterval*i for i in range(-self._bufsize,1)] 35 | # CaGraph stuff 36 | self.graph = Ca.Graph() 37 | self.xaxis, self.yaxis = (Ca.GraphXAxis(self.graph), Ca.GraphYAxis(self.graph)) 38 | self.xaxis.min=self.x[0] 39 | self.xaxis.max=self.x[-1] 40 | self.graph.axiss.append(self.xaxis) 41 | self.graph.axiss.append(self.yaxis) 42 | self.graph.graph_style.width = size[0] 43 | self.graph.graph_style.height = size[1] 44 | series = Ca.GraphSeriesLine(self.graph, 0, 1) 45 | self.graph.seriess.append(series) 46 | self.graph.grid = Ca.GraphGrid(self.graph, 0, 1) 47 | # Gtk stuff 48 | self.add(self.graph) 49 | self.graph.show() 50 | self.show_all() 51 | 52 | def getdata(self): 53 | frequency = 0.5 54 | noise = random.normalvariate(0., 1.) 55 | new = 10.*math.sin(time.time()*frequency*2*math.pi) + noise 56 | return new 57 | 58 | def updateplot(self): 59 | self.databuffer.append( self.getdata() ) 60 | self.graph.seriess[0].data = list(zip(self.x, self.databuffer)) 61 | self.graph.auto_set_yrange(1) 62 | self.graph.queue_draw() 63 | return True 64 | 65 | def run(self): 66 | GLib.timeout_add(self._interval, self.updateplot ) 67 | Gtk.main() 68 | 69 | if __name__ == '__main__': 70 | 71 | m = DynamicPlotter(sampleinterval=0.05, timewindow=10.) 72 | m.run() 73 | 74 | --------------------------------------------------------------------------------