#Boa:Frame:Frame1

import matplotlib
matplotlib.interactive( True )
matplotlib.use( 'WXAgg' )

import numpy as num
import wx
import wx.aui

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1BUTTON2, wxID_FRAME1PANEL1, 
] = [wx.NewId() for _init_ctrls in range(4)]


#-------------------------------------------------------------------------------
#Begin embedded code sample
#Taken off the example online from embedding in wx:
# http://www.scipy.org/Matplotlib_figure_in_a_wx_panel  (version2 not the old version)    

class PlotPanel (wx.Panel):
    """The PlotPanel has a Figure and a Canvas. OnSize events simply set a 
flag, and the actual resizing of the figure is triggered by an Idle event."""
    def __init__( self, parent, color=None, dpi=None, **kwargs ):
        from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg
        from matplotlib.figure import Figure

        # initialize Panel
        if 'id' not in kwargs.keys():
            kwargs['id'] = wx.ID_ANY
        if 'style' not in kwargs.keys():
            kwargs['style'] = wx.NO_FULL_REPAINT_ON_RESIZE
        wx.Panel.__init__( self, parent, **kwargs )

        # initialize matplotlib stuff
        self.figure = Figure( None, dpi )
        self.canvas = FigureCanvasWxAgg( self, -1, self.figure )
        self.SetColor( color )

        self._SetSize()
        self.draw()

        self._resizeflag = False

        self.Bind(wx.EVT_IDLE, self._onIdle)
        self.Bind(wx.EVT_SIZE, self._onSize)

    def SetColor( self, rgbtuple=None ):
        """Set figure and canvas colours to be the same."""
        if rgbtuple is None:
            rgbtuple = wx.SystemSettings.GetColour( wx.SYS_COLOUR_BTNFACE ).Get()
        clr = [c/255. for c in rgbtuple]
        self.figure.set_facecolor( clr )
        self.figure.set_edgecolor( clr )
        self.canvas.SetBackgroundColour( wx.Colour( *rgbtuple ) )

    def _onSize( self, event ):
        self._resizeflag = True

    def _onIdle( self, evt ):
        if self._resizeflag:
            self._resizeflag = False
            self._SetSize()

    def _SetSize( self ):
        pixels = tuple( self.parent.GetClientSize() )
        self.SetSize( pixels )
        self.canvas.SetSize( pixels )
        self.figure.set_size_inches( float( pixels[0] )/self.figure.get_dpi(),
                                     float( pixels[1] )/self.figure.get_dpi() )

    def draw(self): pass # abstract, to be overridden by child classes

class DemoPlotPanel (PlotPanel):
    """Plots several lines in distinct colors."""
    def __init__( self, parent, point_lists, clr_list, **kwargs ):
        self.parent = parent
        self.point_lists = point_lists
        self.clr_list = clr_list

        # initiate plotter
        PlotPanel.__init__( self, parent, **kwargs )
        self.SetColor( (255,255,255) )

    def draw( self ):
        """Draw data."""
        if not hasattr( self, 'subplot' ):
            self.subplot = self.figure.add_subplot( 111 )

        self.subplot.plot(self.point_lists[0], self.point_lists[1], 'r-o' )
        self.subplot.set_title('The plot title',family='fantasy', name='Comic Sans MS', fontsize = 14, weight='normal')
        self.subplot.set_xlabel("the x axis", family='sans-serif',fontsize = 12, weight='normal')
        self.subplot.set_ylabel("the y axis", family='sans-serif', fontsize = 12, weight='normal')
    
#End the embedding in wxPython example code
#-------------------------------------------------------------------------------



#-------------------------------------------------------------------------------
#Begin simple wxPython frame code
#This frame has an option for adding the graph to an wx.AuiNotebook page
#or directly to a panel, using the buttons.  Using them beyond that will
#cause errors, it's just to get the idea of the bottom of the graph being
#cut off a bit.

class Frame1(wx.Frame):
    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.button1, 0, border=0, flag=0)
        parent.AddWindow(self.button2, 0, border=0, flag=0)

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self._init_coll_boxSizer1_Items(self.boxSizer1)

        self.panel1.SetSizer(self.boxSizer1)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
              pos=wx.Point(396, 204), size=wx.Size(404, 489),
              style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
        self.SetClientSize(wx.Size(396, 455))

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(396, 455),
              style=wx.TAB_TRAVERSAL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1,
              label=u'Add Graph to AUINotebook (press first)', name='button1',
              parent=self.panel1, pos=wx.Point(0, 0), size=wx.Size(256, 23),
              style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnAddToNotebookButton,
              id=wxID_FRAME1BUTTON1)

        self.button2 = wx.Button(id=wxID_FRAME1BUTTON2,
              label=u'Add Graph Directly to Panel (press second)',
              name='button2', parent=self.panel1, pos=wx.Point(0, 23),
              size=wx.Size(256, 25), style=0)
        self.button2.Bind(wx.EVT_BUTTON, self.OnAddDirectlyToPanelButton,
              id=wxID_FRAME1BUTTON2)

        self._init_sizers()

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.SetupAuiNotebook()

    #Set up an AUINotebook 
    def SetupAuiNotebook(self):
        self.nb = wx.aui.AuiNotebook(self.panel1)
        self.boxSizer1.AddWindow(self.nb, 1, border=0, flag=wx.EXPAND)
        self.panel1.Layout()
        
    def OnAddToNotebookButton(self, event):
        
        points = [ [1,2,3,4,5], [10, 42, 30, 27, 35] ]
        clrs = [[225,200,160], [219,112,147]]

        graph = DemoPlotPanel( self.nb, points, clrs )
        self.nb.AddPage(graph, 'graph')

    def OnAddDirectlyToPanelButton(self, event):
        self.nb.Destroy()
        points = [ [1,2,3,4,5], [10, 42, 30, 27, 35] ]
        clrs = [[225,200,160], [219,112,147]]

        graph = DemoPlotPanel( self.panel1, points, clrs )
        self.boxSizer1.AddWindow(graph, 1, border=0, flag=wx.EXPAND)
        self.panel1.Layout()

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = create(None)
    frame.Show()

    app.MainLoop()
