We want to make some ellipses!
First we need matplotlib
for plotting.
import matplotlib.pyplot as plt
%matplotlib inline
Note: IPython's built-in widgets only work in a 'live' notebook, not on nbviewer.org.
So we're going to want widgets
. We need patches
for easy ways to make shapes.
from IPython.html import widgets
from matplotlib.patches import Ellipse
:0: FutureWarning: IPython widgets are experimental and may change in the future.
Now we define a function for the plotting, so we can call it with a widget.
The function doesn't return anything; the plot is a side-effect.
def ellipse(**kwargs):
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = Ellipse(xy=(0.5,0.5), **kwargs)
ax.add_patch(ellipse)
plt.show()
Now we can call the function, passing the arguments for the Ellipse
object as kwargs
(short for keyword arguments).
widgets.interactive(ellipse, width=0.33, height=0.1, angle=90)
ipywidgets
pre-renders everything. So it's slow to make, fast to run... and it runs for anyone, even in static servers like nbviewer.org.
from ipywidgets import StaticInteract, RangeWidget
def ellipse(**kwargs):
fig = plt.figure()
ax = fig.add_subplot(111)
ellipse = Ellipse(xy=(0.5,0.5), **kwargs)
ax.add_patch(ellipse)
return fig
width = RangeWidget(0.0, 1.0, 0.2)
height = RangeWidget(0.0, 1.0, 0.2)
angle = RangeWidget(0, 90, 15)
StaticInteract(ellipse, width=width, height=height, angle=angle)