%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(16, 8))
ax0 = plt.subplot2grid((1, 2), (0, 0))
ax1 = plt.subplot2grid((1, 2), (0, 1), polar=True)
x = np.linspace(0, 2 * np.pi, 1000)
y = np.sin(x**2)
ax0.plot(x, y)
plt.polar(x, y, color="r")
plt.show()
Courtesy of MathJax, you can include mathematical expressions both inline: $e^{i\pi} + 1 = 0$ and displayed:
$$e^x=\sum_{i=0}^\infty \frac{1}{i!}x^i$$Because Markdown is a superset of HTML you can even add things like HTML tables:
Header 1 | Header 2 |
---|---|
row 1, cell 1 | row 1, cell 2 |
row 2, cell 1 | row 2, cell 2 |
If you have local files in your Notebook directory, you can refer to these files in Markdown cells via relative URLs.
For example, in the figs
subdirectory there is a Python logo:
and a video with the HTML5 video tag (WAT: A lightning talk by Gary Bernhardt from CodeMash 2012):
The display
function is tool for displaying images, videos, web pages, etc.
from IPython.display import Image
logo = Image(filename='figs/python-logo.png')
logo
Image(url='https://www.python.org/static/community_logos/python-logo-master-v3-TM-flattened.png',
embed=True)
from IPython.display import YouTubeVideo
#https://www.youtube.com/watch?v=iapcKVn7DdY
YouTubeVideo('iapcKVn7DdY')
You can even embed an entire page from another site in an iframe; for example this is the Ipython webpage:
from IPython.display import IFrame
IFrame('http://ipython.org/notebook.html', 800, 400)
IPython includes an architecture for interactive widgets that tie together Python code running in the kernel and JavaScript/HTML/CSS running in the browser. These widgets enable users to explore their code and data interactively.
How does this work?
interact
are "widget abbreviations"Widget
instancesWidgets
objects are Python objects that are automatically synchronized with JavaScript MVC objects running in the browser.interact
simply calls its callable each time any widget changes statefrom IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets
:0: FutureWarning: IPython widgets are experimental and may change in the future.
def f(x):
print(x)
res = interact(f, x=True)
True
res = interact(lambda x: print(x**2),
x=10)
100
res = interact(lambda x: print(x**2),
x=(0, 10, 2))
16
res = interact(f, x='Hello World!')
Hello World!
res = interact(f, x=dict(apple='fruit', orange='color', guitar='instrument'))
instrument
@interact(x=True, y=1.0)
def f(x, y):
print(x, y**2 if x else y)
True 1.0
def f(x, y):
print(x + y)
res = interact(f, x=5, y=fixed(20))
25
The model has the following constants:
We will change the following variables:
# Zombie apocalypse modeling
from scipy.integrate import odeint
d = 0.0001 # natural death percent (per day)
B = 0.0095 # transmission percent (per day)
G = 0.0001 # resurect percent (per day)
A = 0.0001 # destroy percent (per day)
t = np.linspace(0, 5., 1000) # time grid
# Solve the system dy/dt = f(y, t)
def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
P = y[3]
# The model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]
def zombie_apocalypse(initial_human_population,
initial_zombie_population,
initial_death_population,
daily_new_births):
# initial condition vector
y0 = [initial_human_population, initial_zombie_population, initial_death_population, daily_new_births]
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]
# Plot the apocalypse
plt.figure()
plt.plot(t, S, label='Living')
plt.plot(t, Z, label='Zombies')
plt.xlabel('Days from outbreak')
plt.ylabel('Population')
plt.title('Zombie Apocalypse')
plt.legend(loc=0)
res = interact(zombie_apocalypse,
initial_human_population=widgets.IntSlider(min=500, max=1000, step=50, value=500),
initial_zombie_population=fixed(0),
initial_death_population=widgets.IntSlider(min=0, max=100, step=10, value=0),
daily_new_births=widgets.IntSlider(min=0, max=10, step=1, value=0)
)
x = np.random.normal(2, 2, 10000)
y = np.random.normal(10, 5, 10000)
def do_histogram(x):
n, bins, patches = plt.hist(x, normed=1)
plt.show()
do_histogram(x)
import seaborn as sns
do_histogram(x)
p = sns.distplot(x, hist=True, rug=False)
sns.set_context('poster')
p = sns.distplot(x, hist=True, rug=False)
sns.set_context('notebook')
p = sns.kdeplot(x)
p = sns.kdeplot(x, y)
from bokeh import mpl
from bokeh.plotting import output_notebook, show
# Just plot seaborn kde
sns.kdeplot(x)
show(mpl.to_bokeh(notebook=True))
sns.set_context('poster')
sns.reset_orig()
with plt.xkcd():
p = sns.distplot(x, hist=True, rug=False)