Discussion:
Automatic Update of matplotlib embedded graph
Andrea Mastrangelo
2014-09-30 21:40:41 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Werner
2014-10-01 06:26:27 UTC
Permalink
Hi Andrea,
Post by Andrea Mastrangelo
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
Save a reference to grafico_1 (which is an mpl.axes instance -
http://matplotlib.org/api/axes_api.html), 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
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-10-01 16:56:40 UTC
Permalink
Post by Werner
Calling then the update method in the interval you need.
I am not sure if this is something that happens automatically with
matplotlib, but if not, you might look at wx.Timer
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrea Mastrangelo
2014-10-01 20:07:52 UTC
Permalink
Thank you very much, I will try with your hints!
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrea Mastrangelo
2014-10-01 22:48:34 UTC
Permalink
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...
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Nathan McCorkle
2014-10-01 23:10:28 UTC
Permalink
Post by Andrea Mastrangelo
Dears,
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?
http://wiki.wxpython.org/Passing%20Arguments%20to%20Callbacks
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tim Roberts
2014-10-02 18:09:37 UTC
Permalink
Post by Andrea Mastrangelo
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, ***@probo.com
Providenza & Boekelheide, Inc.
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Andrea Mastrangelo
2014-10-03 20:32:05 UTC
Permalink
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()
--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...