Skip to content

Instantly share code, notes, and snippets.

@athornton
Created July 23, 2019 21:31
Show Gist options
  • Save athornton/b3f7757854a92181aa54318d95ff9512 to your computer and use it in GitHub Desktop.
Save athornton/b3f7757854a92181aa54318d95ff9512 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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": 1,
"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"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f5273a11b53744a1940545237f95786a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FigureCanvasNbAgg()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Testing matplotlib interactions with a simple plot\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"plt.figure(1)\n",
"plt.plot(np.sin(np.linspace(0, 20, 100)))\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2f44adb1bc4d4423a553e5755954899c",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FigureCanvasNbAgg()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"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",
"fig.canvas.layout.max_width = '800px'\n",
"\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "8363a08d76dc4d5e8ce6f1b47a85b601",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"FigureCanvasNbAgg()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# A more complex example from the matplotlib gallery\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\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": 5,
"metadata": {},
"outputs": [],
"source": [
"fig.canvas.toolbar_position = 'right'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"fig.canvas.toolbar_visible = False"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "002cf7ad72c04039b5b278c5b9536ab4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"HBox(children=(FloatSlider(value=1.0, max=2.0, min=0.02, orientation='vertical'), FigureCanvasNbAgg()))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# When using the `widget` backend from ipympl,\n",
"# fig.canvas is a proper Jupyter interactive widget, which can be embedded in\n",
"# Layout classes like HBox and Vbox.\n",
"\n",
"# One can bound figure attributes to other widget values.\n",
"\n",
"from ipywidgets import HBox, FloatSlider\n",
"\n",
"plt.ioff()\n",
"plt.clf()\n",
"\n",
"slider = FloatSlider(\n",
" orientation='vertical',\n",
" value=1.0,\n",
" min=0.02,\n",
" max=2.0\n",
")\n",
"\n",
"fig = plt.figure(3)\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",
" 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",
"HBox([slider, fig.canvas])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "LSST",
"language": "python",
"name": "lsst"
},
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment