{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Matplotlib Jupyter Widget Backend\n", "\n", "Enabling interaction with matplotlib charts in the Jupyter notebook and JupyterLab\n", "\n", "https://github.com/matplotlib/jupyter-matplotlib" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Enabling the `widget` backend.\n", "# This requires jupyter-matplotlib a.k.a. ipympl.\n", "# ipympl can be install via pip or conda.\n", "%matplotlib widget\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Testing matplotlib interactions with a simple plot\n", "fig = plt.figure()\n", "plt.plot(np.sin(np.linspace(0, 20, 100)));" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.toolbar_visible = False\n", "fig.canvas.header_visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.footer_visible = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.resizable = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.capture_scroll = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3D plotting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from mpl_toolkits.mplot3d import axes3d\n", "\n", "fig = plt.figure()\n", "ax = fig.add_subplot(111, projection='3d')\n", "\n", "# Grab some test data.\n", "X, Y, Z = axes3d.get_test_data(0.05)\n", "\n", "# Plot a basic wireframe.\n", "ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Subplots" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# A more complex example from the matplotlib gallery\n", "np.random.seed(0)\n", "\n", "n_bins = 10\n", "x = np.random.randn(1000, 3)\n", "\n", "fig, axes = plt.subplots(nrows=2, ncols=2)\n", "ax0, ax1, ax2, ax3 = axes.flatten()\n", "\n", "colors = ['red', 'tan', 'lime']\n", "ax0.hist(x, n_bins, density=1, histtype='bar', color=colors, label=colors)\n", "ax0.legend(prop={'size': 10})\n", "ax0.set_title('bars with legend')\n", "\n", "ax1.hist(x, n_bins, density=1, histtype='bar', stacked=True)\n", "ax1.set_title('stacked bar')\n", "\n", "ax2.hist(x, n_bins, histtype='step', stacked=True, fill=False)\n", "ax2.set_title('stack step (unfilled)')\n", "\n", "# Make a multiple-histogram of data-sets with different length.\n", "x_multi = [np.random.randn(n) for n in [10000, 5000, 2000]]\n", "ax3.hist(x_multi, n_bins, histtype='bar')\n", "ax3.set_title('different sample sizes')\n", "\n", "fig.tight_layout()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.toolbar_position = 'right'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.canvas.toolbar_visible = False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactions with other widgets and layouting" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# When using the `widget` backend from ipympl,\n", "# fig.canvas is a proper Jupyter interactive widget, which can be embedded in\n", "# an ipywidgets layout. See https://ipywidgets.readthedocs.io/en/stable/examples/Layout%20Templates.html\n", "\n", "# One can bound figure attributes to other widget values.\n", "from ipywidgets import AppLayout, FloatSlider\n", "\n", "plt.ioff()\n", "\n", "slider = FloatSlider(\n", " orientation='horizontal',\n", " description='Factor:',\n", " value=1.0,\n", " min=0.02,\n", " max=2.0\n", ")\n", "\n", "slider.layout.margin = '0px 30% 0px 30%'\n", "slider.layout.width = '40%'\n", "\n", "fig = plt.figure()\n", "fig.canvas.header_visible = False\n", "fig.canvas.layout.min_height = '400px'\n", "plt.title('Plotting: y=sin({} * x)'.format(slider.value))\n", "\n", "x = np.linspace(0, 20, 500)\n", "\n", "lines = plt.plot(x, np.sin(slider.value * x))\n", "\n", "def update_lines(change):\n", " plt.title('Plotting: y=sin({} * x)'.format(change.new))\n", " lines[0].set_data(x, np.sin(change.new * x))\n", " fig.canvas.draw()\n", " fig.canvas.flush_events()\n", "\n", "slider.observe(update_lines, names='value')\n", "\n", "AppLayout(\n", " center=fig.canvas,\n", " footer=slider,\n", " pane_heights=[0, 6, 1]\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.1" } }, "nbformat": 4, "nbformat_minor": 4 }