Automatic Update of matplotlib embedded graph

I i’m a newbie of python, wxpython and matplotlib.
I’ve a question about the update of a drawer graph, how can i do it?

i wrote that, it works, but i don’t know if it’s the best way!

figura = matplotlib.figure.Figure(figsize=(5,5))

grafico_1 = figura.add_subplot(211)
grafico_1.plot(x,y)

canvas = FigureCanvas (MainWindow, -1, figura)

#updatedatas

grafico_1.hold(False)

grafico_1.plot((1,2,3,4),(1,2,3,1))

canvas.draw()

``

Then i need to call automatically this function too, i need a call every seconds, how can i do?

Thank you for helping me and excuse for my bad english

Andrea

Save a reference to grafico_1 (which is an mpl.axes instance -
), i.e. call it
self.grafico_1 and then in your update method do:
self.grafico_1.clear()
self.grafico_1.plot(new info)
self.canvas.draw()
Calling then the update method in the interval you need.
Hope this helps
Werner

···

Hi Andrea,

  On 9/30/2014 23:40, Andrea Mastrangelo wrote:

I i’m a newbie of python, wxpython and matplotlib.
I’ve a question about the update of a drawer graph, how can
i do it?

      i wrote that, it works, but i don't know if it's the best

way!

              figura

=
matplotlib.figure.Figure(figsize=(5,5))

              grafico_1

=
figura.add_subplot(211)

              grafico_1.plot(x,y)
              canvas

= FigureCanvas (MainWindow, -1,
figura)

#updatedatas

grafico_1.hold(False)

grafico_1.plot((1,2,3,4),(1,2,3,1))

canvas.draw()

``

      Then i need to call automatically this function too, i need a

call every seconds, how can i do?

Thank you for helping me and excuse for my bad english

http://matplotlib.org/api/axes_api.html

I am not sure if this is something that happens automatically with matplotlib, but if not, you might look at wx.Timer

···

On Tuesday, September 30, 2014 11:26:29 PM UTC-7, werner wrote:

Calling then the update method in the interval you need.

Thank you very much, I will try with your hints!

Dears,
i’ve done something but i’ve a little problem:

def onTimer(self):

s = ser.read(byte)

a.append(float(s))

b.append(sin(pi*float(s)))

grafico_1.clear()

grafico_1.plot(a,b)

canvas.draw()

print float(s),sin(pi*float(s))

wx.EVT_TIMER(finestra_2, 100, onTimer)

``

I create some buttons to start and stop mi repetitive function and it works when it’s written like posted code

but i need to pass some parameters to onTimer and if i pass any parameter to onTimer the function will work a time automatically (not call from the button), and nothing more.

Can i pass any parameters to that function?

I upload all the file, i don’t know if you can understand anything because i tried many things…

serialGraph.py (2.6 KB)

http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks

···

On Wednesday, October 1, 2014 3:48:34 PM UTC-7, Andrea Mastrangelo wrote:

Dears,
i’ve done something but i’ve a little problem:

def onTimer(self):

s = ser.read(byte)
a.append(float(s))
b.append(sin(pi*float(s)))
grafico_1.clear()
grafico_1.plot(a,b)
canvas.draw()
print float(s),sin(pi*float(s))

wx.EVT_TIMER(finestra_2, 100, onTimer)

``

I create some buttons to start and stop mi repetitive function and it works when it’s written like posted code

but i need to pass some parameters to onTimer and if i pass any parameter to onTimer the function will work a time automatically (not call from the button), and nothing more.

Can i pass any parameters to that function?

Andrea Mastrangelo wrote:

I create some buttons to start and stop mi repetitive function and it
works when it's written like posted code

but i need to pass some parameters to onTimer and if i pass any
parameter to onTimer the function will work a time automatically (not
call from the button), and nothing more.

Can i pass any parameters to that function?

In my opinion, it would be better for you to set a member variable that
tells the timer callback what to do. So:

    def onStart(self, event):
        self.timerOption = 1
        self.timer.Start(1000)

    def OnTimer(self, event):
        if self.timerOption == 1:
            # Do one thing
        elif self.timerOption == 2:
            # Do a different thing

If you might have several things to do, you could set up a list and
append to the list.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Ok, thank you for your hint!

I’ve solved in this way, but i will use a list of veld.variable to check what to do

import wx

import serial

import matplotlib

from matplotlib.figure import Figure

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas

from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar

from numpy import arange, sin, pi

porta_1 = ‘/dev/tty.usbmodem1411’

porta_2 = ‘/dev/tty.usbmodem1421’

baudrate = 9600

class UnderWindow(wx.Panel):

def init(self, parent, size):

wx.Panel.init(self, parent, size = size)

class MainWindow(wx.Frame):

def init(self, parent, size, serial):

wx.Frame.init(self, parent, size=size)

if serial:

self.x, self.y=[],[]

self.x.append(0.0)

self.y.append(0.0)

self.byte = 1

sizer = wx.BoxSizer(wx.VERTICAL)

sizerGrid = wx.GridSizer(rows=3, cols=2, hgap=5, vgap=5) #insert grid sizer

startButton = wx.Button(self, -1, ‘Start’)

startButton.Bind(wx.EVT_BUTTON, self.onStart)

stopButton = wx.Button(self, -1, ‘Stop’)

stopButton.Bind(wx.EVT_BUTTON,self.onStop)

openButton=wx.Button(self, -1, ‘Open’)

openButton.Bind(wx.EVT_BUTTON,self.onOpen)

closeButton=wx.Button(self, -1, ‘Close’)

closeButton.Bind(wx.EVT_BUTTON,self.closeSerial)

self.innerpanel = UnderWindow(self, (800,400))

sizerGrid.Add(openButton, 0, wx.EXPAND)

sizerGrid.Add(closeButton, 0, wx.EXPAND)

sizerGrid.Add(startButton, 0, wx.EXPAND)

sizerGrid.Add(stopButton, 0, wx.EXPAND)

sizer.Add(sizerGrid,0, wx.EXPAND)

sizer.Add(self.innerpanel,0, wx.EXPAND)

self.SetSizer(sizer)

sizer.Fit(self)

figura = matplotlib.figure.Figure(figsize=(10,5))

self.grafico_1 = figura.add_subplot(111)

self.grafico_1.set_title(‘Prova’, size=10)

self.grafico_1.set_xlabel(‘x’, size=8)

self.grafico_1.set_xscale(‘linear’) #log, symlog

self.grafico_1.set_ylabel(‘y’, size=8)

self.canvas = FigureCanvas (self.innerpanel, -1, figura)

toolbar = NavigationToolbar(self.canvas)

toolbar.Hide()

self.timer = wx.Timer (self.innerpanel, 100)

wx.EVT_TIMER(self.innerpanel, 100, self.onTimer)

def onStart(self, event):

self.timer.Start(100) # x100 milliseconds

self.ser.write(‘1’)

def onStop(self, event):

self.timer.Stop()

self.ser.write(‘0’)

def onOpen(self,event):

self.openSerial()

def closeSerial(self,event):

self.timer.Stop()

self.ser.close()

def onTimer(self, event):

self.num=’’

s = self.ser.read(self.byte)

if s == ‘s’:

s = self.ser.read(self.byte)

while s != ‘*’:

self.num += s

s = self.ser.read(self.byte)

print self.num

self.num = float(self.num)

self.x.append(self.num)

self.y.append(sin(pi*self.num))

self.grafico_1.clear()

self.grafico_1.plot(self.x,self.y)

self.canvas.draw()

def openSerial(self):

self.ser = serial.Serial(porta_1, baudrate, timeout = 0.05)

#pulisce la seriale dei primi dati che possono essere ‘sporchi’

s = self.ser.read(self.byte)

s = self.ser.read(self.byte)

s = self.ser.read(self.byte)

s = self.ser.read(self.byte)

app = wx.App()

finestra = MainWindow(None, (300,300), True)

finestra.Show()

app.MainLoop()

``