Plotting data from serial stream

Hello,

I read a simple data stream from my computers serial port. I can nicely read the data using pyserial library but couldn’t get it nicely working neither with WXAgg nor Qt4Agg using the following code. Although with WX I could get updated looks, the figure isn’t very responsive in this way. I have looked at simple_idle_wx example but for some reason I can’t make it work to update the canvas whenever the condition satisfied.

What is the trick to make the real-time data plotted on the screen easily without blocking the figure itself?

import serial
import matplotlib.pyplot as plt
plt.ion()

ser = serial.Serial(0)

conc = []
while True:
s = ser.readline()
if s.startswith(‘CONC’):
conc.append(float(s.split()[2]))
plt.plot(conc)
plt.show()
plt.clf()

ser.close()

···


Gökhan

I’ve done this successfully with both the Qt4 and WX Agg backends. The part you care about is the update method.

Here’s the snippet I use for Qt. It shouldn’t change except for your imports at the top if you’re using Wx. Note, I’ve trimmed this down a bit, so hopefully I didn’t trim out anything important:

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class PlotWidget(QWidget):

def __init__(self, parent):
    QWidget.__init__(self, parent)

    self.dpi=72
    self.figure = Figure(dpi=self.dpi, linewidth=3)

    self.canvas = FigureCanvas(self.figure)
    self.canvas.setParent(self)

    [self.ax](http://self.ax) = self.figure.add_subplot(111)

    (self.__times, self.__temps) = ([], [])
    self.background = self.canvas.copy_from_bbox(self.ax.bbox)
    self.plotting_line = self.ax.plot([], color='red', lw=2)[0]

def update(self, temp, secs):
    try:
        self.__times.append(secs)
        self.__temps.append(temp)

        self.canvas.restore_region(self.background)

        self.plotting_line.set_xdata(self.__times)
        self.plotting_line.set_ydata(self.__temps)

        self.ax.draw_artist(self.plotting_line)
        self.canvas.blit(self.ax.bbox)
    except IndexError:
        pass

    self.canvas.draw()
···

On Fri, Mar 12, 2010 at 12:36 PM, Gökhan Sever <gokhansever@…287…> wrote:

Hello,

I read a simple data stream from my computers serial port. I can nicely read the data using pyserial library but couldn’t get it nicely working neither with WXAgg nor Qt4Agg using the following code. Although with WX I could get updated looks, the figure isn’t very responsive in this way. I have looked at simple_idle_wx example but for some reason I can’t make it work to update the canvas whenever the condition satisfied.

What is the trick to make the real-time data plotted on the screen easily without blocking the figure itself?

import serial
import matplotlib.pyplot as plt
plt.ion()

ser = serial.Serial(0)

conc = []

while True:
s = ser.readline()
if s.startswith(‘CONC’):
conc.append(float(s.split()[2]))
plt.plot(conc)
plt.show()
plt.clf()

ser.close()


Gökhan


Download Intel® Parallel Studio Eval

Try the new software tools for yourself. Speed compiling, find bugs

proactively, and fine-tune applications for parallel performance.

See why Intel Parallel Studio got high marks during beta.

http://p.sf.net/sfu/intel-sw-dev


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users