{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Embed in wx #3\n\nCopyright (C) 2003-2004 Andrew Straw, Jeremy O'Donoghue and others\n\nLicense: This work is licensed under the PSF. A copy should be included\nwith this source code, and is also available at\nhttps://docs.python.org/3/license.html\n\nThis is yet another example of using matplotlib with wx.  Hopefully\nthis is pretty full-featured:\n\n- both matplotlib toolbar and WX buttons manipulate plot\n- full wxApp framework, including widget interaction\n- XRC (XML wxWidgets resource) file to create GUI (made with XRCed)\n\nThis was derived from embedding_in_wx and dynamic_image_wxagg.\n\nThanks to matplotlib and wx teams for creating such great software!\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import wx\nimport wx.xrc as xrc\n\nimport numpy as np\n\nfrom matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas\nfrom matplotlib.backends.backend_wxagg import \\\n    NavigationToolbar2WxAgg as NavigationToolbar\nimport matplotlib.cbook as cbook\nfrom matplotlib.figure import Figure\n\nERR_TOL = 1e-5  # floating point slop for peak-detection\n\n\nclass PlotPanel(wx.Panel):\n    def __init__(self, parent):\n        super().__init__(parent, -1)\n\n        self.fig = Figure((5, 4), 75)\n        self.canvas = FigureCanvas(self, -1, self.fig)\n        self.toolbar = NavigationToolbar(self.canvas)  # matplotlib toolbar\n        self.toolbar.Realize()\n\n        # Now put all into a sizer\n        sizer = wx.BoxSizer(wx.VERTICAL)\n        # This way of adding to sizer allows resizing\n        sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)\n        # Best to allow the toolbar to resize!\n        sizer.Add(self.toolbar, 0, wx.GROW)\n        self.SetSizer(sizer)\n        self.Fit()\n\n    def init_plot_data(self):\n        ax = self.fig.add_subplot()\n\n        x = np.arange(120.0) * 2 * np.pi / 60.0\n        y = np.arange(100.0) * 2 * np.pi / 50.0\n        self.x, self.y = np.meshgrid(x, y)\n        z = np.sin(self.x) + np.cos(self.y)\n        self.im = ax.imshow(z, cmap=\"RdBu\", origin='lower')\n\n        zmax = np.max(z) - ERR_TOL\n        ymax_i, xmax_i = np.nonzero(z >= zmax)\n        if self.im.origin == 'upper':\n            ymax_i = z.shape[0] - ymax_i\n        self.lines = ax.plot(xmax_i, ymax_i, 'ko')\n\n        self.toolbar.update()  # Not sure why this is needed - ADS\n\n    def GetToolBar(self):\n        # You will need to override GetToolBar if you are using an\n        # unmanaged toolbar in your frame\n        return self.toolbar\n\n    def OnWhiz(self, event):\n        self.x += np.pi / 15\n        self.y += np.pi / 20\n        z = np.sin(self.x) + np.cos(self.y)\n        self.im.set_array(z)\n\n        zmax = np.max(z) - ERR_TOL\n        ymax_i, xmax_i = np.nonzero(z >= zmax)\n        if self.im.origin == 'upper':\n            ymax_i = z.shape[0] - ymax_i\n        self.lines[0].set_data(xmax_i, ymax_i)\n\n        self.canvas.draw()\n\n\nclass MyApp(wx.App):\n    def OnInit(self):\n        xrcfile = cbook.get_sample_data('embedding_in_wx3.xrc',\n                                        asfileobj=False)\n        print('loading', xrcfile)\n\n        self.res = xrc.XmlResource(xrcfile)\n\n        # main frame and panel ---------\n\n        self.frame = self.res.LoadFrame(None, \"MainFrame\")\n        self.panel = xrc.XRCCTRL(self.frame, \"MainPanel\")\n\n        # matplotlib panel -------------\n\n        # container for matplotlib panel (I like to make a container\n        # panel for our panel so I know where it'll go when in XRCed.)\n        plot_container = xrc.XRCCTRL(self.frame, \"plot_container_panel\")\n        sizer = wx.BoxSizer(wx.VERTICAL)\n\n        # matplotlib panel itself\n        self.plotpanel = PlotPanel(plot_container)\n        self.plotpanel.init_plot_data()\n\n        # wx boilerplate\n        sizer.Add(self.plotpanel, 1, wx.EXPAND)\n        plot_container.SetSizer(sizer)\n\n        # whiz button ------------------\n        whiz_button = xrc.XRCCTRL(self.frame, \"whiz_button\")\n        whiz_button.Bind(wx.EVT_BUTTON, self.plotpanel.OnWhiz)\n\n        # bang button ------------------\n        bang_button = xrc.XRCCTRL(self.frame, \"bang_button\")\n        bang_button.Bind(wx.EVT_BUTTON, self.OnBang)\n\n        # final setup ------------------\n        self.frame.Show()\n\n        self.SetTopWindow(self.frame)\n\n        return True\n\n    def OnBang(self, event):\n        bang_count = xrc.XRCCTRL(self.frame, \"bang_count\")\n        bangs = bang_count.GetValue()\n        bangs = int(bangs) + 1\n        bang_count.SetValue(str(bangs))\n\n\nif __name__ == '__main__':\n    app = MyApp()\n    app.MainLoop()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.14.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}