{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Matplotlib embedded in GUI\n\nThis examples shows how the ``matplotlib`` can be integrated\ninto a ``GUI`` based on the ``MatplotlibPlot`` Qt Designer plugin. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\nimport matplotlib.pyplot as plt\nfrom itom import ui"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plots spines into the ``MatplotlibPlot`` Qt Designer plugin.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def plotDroppedSpines():\n \"\"\"\n plot taken from matplotlib example 'spines_demo_dropped.py'\n \"\"\"\n canvas = gui.plot # reference to matplotlibPlot widget\n\n # if the same figure object in the matplotlib figure manager should\n # be reused, since it is assigned to the pre-defined canvas in the ui\n # file, you need to always set a unique number (can be arbitrary, but unique)\n fig = plt.figure(num=3, 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 image = np.random.uniform(size=(10, 10))\n ax.imshow(image, cmap=plt.cm.gray, interpolation=\"nearest\")\n ax.set_title(\"dropped spines\")\n\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\n plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plots a sine curve into the ``MatplotlibPlot`` Qt Designer plugin.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def plotSine():\n \"\"\"\n plots sine, taken from matplotlib gallery examples\n \"\"\"\n t = np.arange(0.0, 1.0, 0.01)\n s = np.sin(2 * np.pi * t)\n\n canvas = gui.plot # reference to matplotlibPlot widget\n fig = plt.figure(num=3, 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.plot(t, s)\n\n plt.show()\n\n\ngui = ui(\"matplotlibGui.ui\", type=ui.TYPEWINDOW)\ngui.btnSine.connect(\"clicked()\", plotSine)\ngui.btnDroppedSpines.connect(\"clicked()\", plotDroppedSpines)\ngui.show()\n\n# if you call this script for the second time, the given figure-num (3)\n# is already in used for the lastly closed figure. Therefore also tell\n# matplotlib to close this figure handle.\nplt.close(3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n"
]
}
],
"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
}