{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Embed in wx #4\n\nAn example of how to use wxagg in a wx application with a custom toolbar.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import wx\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\nfrom matplotlib.figure import Figure\n\n\nclass MyNavigationToolbar(NavigationToolbar):\n    \"\"\"Extend the default wx toolbar with your own event handlers.\"\"\"\n\n    def __init__(self, canvas):\n        super().__init__(canvas)\n        # We use a stock wx bitmap, but you could also use your own image file.\n        bmp = wx.ArtProvider.GetBitmap(wx.ART_CROSS_MARK, wx.ART_TOOLBAR)\n        tool = self.AddTool(wx.ID_ANY, 'Click me', bmp,\n                            'Activate custom control')\n        self.Bind(wx.EVT_TOOL, self._on_custom, id=tool.GetId())\n\n    def _on_custom(self, event):\n        # add some text to the Axes in a random location in axes coords with a\n        # random color\n        ax = self.canvas.figure.axes[0]\n        x, y = np.random.rand(2)  # generate a random location\n        rgb = np.random.rand(3)  # generate a random color\n        ax.text(x, y, 'You clicked me', transform=ax.transAxes, color=rgb)\n        self.canvas.draw()\n        event.Skip()\n\n\nclass CanvasFrame(wx.Frame):\n    def __init__(self):\n        super().__init__(None, -1, 'CanvasFrame', size=(550, 350))\n\n        self.figure = Figure(figsize=(5, 4), dpi=100)\n        self.axes = self.figure.add_subplot()\n        t = np.arange(0.0, 3.0, 0.01)\n        s = np.sin(2 * np.pi * t)\n\n        self.axes.plot(t, s)\n\n        self.canvas = FigureCanvas(self, -1, self.figure)\n\n        self.sizer = wx.BoxSizer(wx.VERTICAL)\n        self.sizer.Add(self.canvas, 1, wx.TOP | wx.LEFT | wx.EXPAND)\n\n        self.toolbar = MyNavigationToolbar(self.canvas)\n        self.toolbar.Realize()\n        # By adding toolbar in sizer, we are able to put it at the bottom\n        # of the frame - so appearance is closer to GTK version.\n        self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.EXPAND)\n\n        # update the axes menu on the toolbar\n        self.toolbar.update()\n        self.SetSizer(self.sizer)\n        self.Fit()\n\n\nclass App(wx.App):\n    def OnInit(self):\n        \"\"\"Create the main window and insert the custom frame.\"\"\"\n        frame = CanvasFrame()\n        frame.Show(True)\n        return True\n\n\nif __name__ == \"__main__\":\n    app = App()\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
}