#!/usr/bin/env python
# -*- coding: ISO-8859-15 -*-

import matplotlib
matplotlib.use('WXAgg')
from matplotlib.backends.backend_wxagg import Toolbar, FigureCanvasWxAgg,NavigationToolbar2Wx,NavigationToolbar2WxAgg
from matplotlib.figure import Figure
from random import random
from wxPython.wx import *

import wx,numpy
TIMER_ID = wxNewId()


class PlotPanel(wxPanel):
    def __init__(self, parent):
        wxPanel.__init__(self, parent, -1)
        self.fig = Figure((8,6), 75)
        self.canvas = FigureCanvasWxAgg(self, -1, self.fig)	
        self.toolbar = NavigationToolbar2WxAgg(self.canvas) #matplotlib toolbar
        self.toolbar.Realize()
        # Now put all into a sizer
        sizer = wxBoxSizer(wxVERTICAL)
        # This way of adding to sizer allows resizing
        sizer.Add(self.canvas, 1, wxLEFT|wxTOP|wxGROW)
        # Best to allow the toolbar to resize!
        sizer.Add(self.toolbar, 0, wxGROW)
        self.SetSizer(sizer)
        self.Fit()
	self.__init_plot()
	#self.update_plot_data()
	EVT_TIMER(self, TIMER_ID, self.update_plot_data)
	#EVT_TIMER(parent, TIMER_ID, self.update_plot_data)

    def __init_plot(self):
        # Use ths line if using a toolbar
        self.a = self.fig.add_subplot(111)
        self.a.set_title('Medida M(T)')
        self.a.set_xlabel('Temperatura [K]')
        self.a.set_ylabel('Magnetisacao [u.a.]')
        self.lines = self.a.plot([],'o')
	#self.a.set_autoscale_on(True) 

    def update_plot_data(self,ll):
        self.lines = self.a.plot([],'+')
	t = numpy.arange(0.0,3.14,0.01)
        s = numpy.sin(2*numpy.pi*(t+random()))
	del self.a.lines[0]
	self.lines[0].set_data(t, s)
        self.a.set_xlim(min(t),max(t))
        self.a.set_ylim(min(s),max(s))
        self.toolbar.update()
	self.canvas.draw()
	print "update_plot"

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.label_1 = wx.StaticText(self, -1, "blablablabla")
        self.panel_graf = PlotPanel(self)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle("test matplotlib embbeded")
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.label_1, 0, wx.ADJUST_MINSIZE, 0)
        sizer_1.Add(self.panel_graf, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        self.Layout()
        # end wxGlade
# end of class MyFrame

class MyApp(wx.App):
    def OnInit(self):
        wx.InitAllImageHandlers()
        self.frame1 = MyFrame(None, -1, "")
        self.SetTopWindow(self.frame1)
        self.frame1.Show()
        return 1

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    t = wxTimer(app.frame1.panel_graf, TIMER_ID)
    #t = wxTimer(app.frame1, TIMER_ID)
    t.Start(200)
    app.MainLoop()
