Skip to content

Commit 092f93d

Browse files
authored
Merge pull request #78 from martinRenou/add_ipympl1
Add ipympl notebook
2 parents 4926bba + 16c85aa commit 092f93d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# The Matplotlib Jupyter Widget Backend\n",
8+
"\n",
9+
"Enabling interaction with matplotlib charts in the Jupyter notebook and JupyterLab\n",
10+
"\n",
11+
"https://github.com/matplotlib/jupyter-matplotlib"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Enabling the `widget` backend.\n",
21+
"# This requires jupyter-matplotlib a.k.a. ipympl.\n",
22+
"# ipympl can be install via pip or conda.\n",
23+
"%matplotlib widget"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"import matplotlib.pyplot as plt\n",
33+
"import numpy as np"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"# When using the `widget` backend from ipympl,\n",
43+
"# fig.canvas is a proper Jupyter interactive widget, which can be embedded in\n",
44+
"# Layout classes like HBox and Vbox.\n",
45+
"\n",
46+
"# One can bound figure attributes to other widget values.\n",
47+
"\n",
48+
"from ipywidgets import HBox, FloatSlider\n",
49+
"\n",
50+
"plt.ioff()\n",
51+
"plt.clf()\n",
52+
"\n",
53+
"slider = FloatSlider(\n",
54+
" orientation='vertical',\n",
55+
" value=1.0,\n",
56+
" min=0.02,\n",
57+
" max=2.0\n",
58+
")\n",
59+
"\n",
60+
"fig = plt.figure(3)\n",
61+
"\n",
62+
"x = np.linspace(0, 20, 500)\n",
63+
"\n",
64+
"lines = plt.plot(x, np.sin(slider.value * x))\n",
65+
"\n",
66+
"def update_lines(change):\n",
67+
" lines[0].set_data(x, np.sin(change.new * x))\n",
68+
" fig.canvas.draw()\n",
69+
" fig.canvas.flush_events()\n",
70+
"\n",
71+
"slider.observe(update_lines, names='value')\n",
72+
"\n",
73+
"HBox([slider, fig.canvas])"
74+
]
75+
}
76+
],
77+
"metadata": {
78+
"kernelspec": {
79+
"display_name": "Python 3",
80+
"language": "python",
81+
"name": "python3"
82+
},
83+
"language_info": {
84+
"codemirror_mode": {
85+
"name": "ipython",
86+
"version": 3
87+
},
88+
"file_extension": ".py",
89+
"mimetype": "text/x-python",
90+
"name": "python",
91+
"nbconvert_exporter": "python",
92+
"pygments_lexer": "ipython3",
93+
"version": "3.7.3"
94+
}
95+
},
96+
"nbformat": 4,
97+
"nbformat_minor": 4
98+
}

0 commit comments

Comments
 (0)