{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Embed in wx #2\n\nAn example of how to use wxagg in an application with the new\ntoolbar - comment out the add_toolbar line for no toolbar.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import wx\nimport wx.lib.mixins.inspection as WIT\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 CanvasFrame(wx.Frame):\n def __init__(self):\n super().__init__(None, -1, 'CanvasFrame', size=(550, 350))\n\n self.figure = Figure()\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 self.canvas = FigureCanvas(self, -1, self.figure)\n\n self.sizer = wx.BoxSizer(wx.VERTICAL)\n self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.EXPAND)\n self.SetSizer(self.sizer)\n self.Fit()\n\n self.add_toolbar() # comment this out for no toolbar\n\n def add_toolbar(self):\n self.toolbar = NavigationToolbar(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 # update the axes menu on the toolbar\n self.toolbar.update()\n\n\n# Alternatively you could use:\n# class App(wx.App):\nclass App(WIT.InspectableApp):\n def OnInit(self):\n \"\"\"Create the main window and insert the custom frame.\"\"\"\n self.Init()\n frame = CanvasFrame()\n frame.Show(True)\n\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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }