{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matplotlib auto update\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib\nimport matplotlib.pyplot as plt\nfrom itomUi import ItomUi\nfrom itom import ui\n\nmatplotlib.use(\"module://mpl_itom.backend_itomagg\")\n\n\nclass MatplotGuiAutoUpdate(ItomUi):\n def __init__(self):\n ItomUi.__init__(\n self,\n \"matplotlibGuiAutoUpdate.ui\",\n ui.TYPEWINDOW,\n childOfMainWindow=True,\n deleteOnClose=True,\n )\n # the deleteOnClose = True argument will not only hide the figure if the close-button is pressed,\n # but also destroy it. Then, the destroyed signal is emitted, caught by the slot self.on_mainWindow_destroyed.\n # This can be used, to delete certain things.\n\n self.gui.btnStop[\"enabled\"] = False\n self.timerID = None\n self.counter = 1\n self.fignum = (\n max(\n [\n 0,\n ]\n + plt.get_fignums()\n )\n + 1\n ) # guarantee to get a new matplotlib figure number\n\n if not \"__version__\" in ItomUi.__dict__ or ItomUi.__version__ < \"2.1\":\n # in earlier versions of itom, an auto-connection to methods of the gui itself was not possible.\n # From ItomUi.__version__ >= 2.1 on, the dialog or window itself can be auto-connected by its\n # object name.\n self.gui.connect(\"destroyed()\", self.on_mainWindow_destroyed)\n\n @ItomUi.autoslot(\"\")\n def on_btnStart_clicked(self):\n if self.timerID is None:\n # start the timer that calls the method 'timeout' every 2 seconds\n self.timerID = timer(2000, self.timeout)\n self.timeout()\n self.gui.btnStart[\"enabled\"] = False\n self.gui.btnStop[\"enabled\"] = True\n\n @ItomUi.autoslot(\"\")\n def on_btnStop_clicked(self):\n if not self.timerID is None:\n # stop and delete the timer, if started\n self.timerID.stop()\n self.timerID = None\n self.gui.btnStart[\"enabled\"] = True\n self.gui.btnStop[\"enabled\"] = False\n\n # for itom <= 2.1, this auto-slot will raise a runtime error, however it is manually connected in the constructor of this class.\n @ItomUi.autoslot(\"\")\n def on_mainWindow_destroyed(self):\n \"\"\"The windows was closed and destroyed. Stop the timer and tell matplotlib to close the figure\"\"\"\n if not self.timerID is None:\n self.timerID.stop()\n self.timerID = None\n plt.close(self.fignum)\n\n def timeout(self):\n print(\"update plot\")\n\n canvas = self.gui.matplotlibPlot # Reference to matplotlibPlot widget\n fig = plt.figure(num=self.fignum, canvas=canvas)\n\n if len(fig.axes) == 0:\n # create a new subplot in the figure\n ax = fig.add_subplot(111)\n else:\n # reuse the existing first subplot\n ax = fig.axes[0]\n ax.clear()\n \n ax.imshow(dataObject.randN([100, 100], \"uint8\"), cmap=plt.cm.gray)\n ax.set_title(\"title of plot [%i]\" % self.counter)\n self.counter += 1\n # Move left and bottom spines outward by 10 points\n ax.spines[\"left\"].set_position((\"outward\", 10))\n ax.spines[\"bottom\"].set_position((\"outward\", 10))\n # Hide the right and top spines\n ax.spines[\"right\"].set_visible(False)\n ax.spines[\"top\"].set_visible(False)\n # Only show ticks on the left and bottom spines\n ax.yaxis.set_ticks_position(\"left\")\n ax.xaxis.set_ticks_position(\"bottom\")\n plt.show()\n\n def show(self):\n ret = self.gui.show()\n\n\nif __name__ == \"__main__\":\n gui = MatplotGuiAutoUpdate()\n gui.show()" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 0 }