In [1]:
import numpy as np
import matplotlib.pyplot as plt
import mpld3

from IPython.display import HTML, display, YouTubeVideo

# Load CSS
display(HTML(open('style.css').read()))

IPython notebook

widgets and alternative visulisation tools

 

Ivan Zimine
vanzaj (@gmail, github, twitter)
In [2]:
YouTubeVideo('wjSsISiacEU', width=600, height=400)
Out[2]:
In [3]:
def embed_url(url, width=800, height=450):
    src='<iframe src="{0}"  width="{1}" height="{2}"></iframe>'.format(url, 800, 450)
    display(HTML(src))
    
embed_url('http://worrydream.com/ExplorableExplanations/')
In [4]:
embed_url('https://en.wikipedia.org/wiki/Literate_programming')
In [5]:
embed_url('http://ipython.org/notebook.html')

live code cells

In [6]:
import random

# generate a random number between 1000 and 9999
n = random.randint(1e3, 1e4-1)
n*2
Out[6]:
15566

Code cells are executable mini-editors. It's like having a bunch of interactive shells sharing the same namespace.

In [7]:
if n < 5000:
    print('Smaller half')
else:
    print('Bigger half')
Bigger half

live documentation

In [8]:
n = random.randint(1e3, 1e4-1)  # that's 10^3
print(n)
5405

$10^3$ is much better than '10^3' to explan '1e3'.

interaction with OS

In [9]:
%%bash

#echo $PATH

py=$(which python)
echo "Current python executable: $py"
Current python executable: /Users/ivan/dev/pve/dev3/bin/python
In [10]:
!ls ../data/
2013-Nov_chicago-salaries.csv.gz UN_population.csv
20131117_bash_history.txt        countries.json
UN_infant_mortality.csv          population.txt

interaction with OS

In [11]:
fls = !ls ../data/
fls
Out[11]:
['2013-Nov_chicago-salaries.csv.gz',
 '20131117_bash_history.txt',
 'UN_infant_mortality.csv',
 'UN_population.csv',
 'countries.json',
 'population.txt']
In [12]:
!head ../data/{fls[-3]}









rich output

In [13]:
import pandas as pd

df = pd.read_csv('../data/'+fls[-3])
df.head(4)
Out[13]:
Country Subgroup Year Unit Value Value Footnotes
0 Afghanistan Female 2005 Number 12082881 NaN
1 Afghanistan Female 2002 Number 9844300 1
2 Afghanistan Female 2000 Number 9993697 NaN
3 Afghanistan Female 1995 Number 8792425 NaN

rich output

In [14]:
from sympy import init_printing, Symbol, cos, sin
init_printing(use_latex='mathjax')

x = Symbol('x')
print(cos(x).series(x, 0, 7))
1 - x**2/2 + x**4/24 - x**6/720 + O(x**7)
In [15]:
display(cos(x).series(x, 0, 5))
$$1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} + \mathcal{O}\left(x^{5}\right)$$

rich output 2.0

In [16]:
from IPython.html.widgets import interact, ToggleButtonsWidget


def show_series(n):
    display(cos(x).series(x, 0, n))


interact(show_series, n=(3,25));
$$1 - \frac{x^{2}}{2} + \frac{x^{4}}{24} - \frac{x^{6}}{720} + \frac{x^{8}}{40320} - \frac{x^{10}}{3628800} + \frac{x^{12}}{479001600} + \mathcal{O}\left(x^{14}\right)$$
In [17]:
import string

def gen_passwd(length=6, punctuation=False):
    chars = string.ascii_letters + string.digits
    if punctuation:
        chars += string.punctuation
    passwd = [random.choice(chars) for _ in range(length)]
    return ''.join(passwd)


def print_passwd(length=6, punctuation=False):
    print("New password: " + gen_passwd(length, punctuation))


interact(print_passwd, 
         length=(6,20), 
         punctuation={"no": False, "yes":True});
New password: qwR6Fd

html5

In [18]:
from IPython.display import Audio, clear_output

duration = 3
sampling = 8000
ts = np.linspace(0, duration, sampling*duration)

def sig_gen(time, f0=220.0, f1=224.0):
    two_pi = 2*np.pi
    sig = np.sin(two_pi*f0*time) + np.sin(two_pi*f1*time)
    return sig

display(Audio(data=sig_gen(ts), rate=sampling))
In [19]:
def play_sig(f0=220.0, f1=224.0):
    sig = sig_gen(ts, f0, f1)
    display(Audio(data=sig, rate=sampling, autoplay=True))

    
interact(play_sig, f0=(200.0, 300.0), f1=(200.0, 300.0));
In [20]:
%matplotlib inline
In [21]:
def play_sig(f0=220.0, f1=224.0):
    sig = sig_gen(ts, f0, f1)
    display(Audio(data=sig, rate=sampling, autoplay=True))
    plt.plot(ts[:sampling/2], sig[:sampling/2])

    
interact(play_sig, f0=(200.0, 300.0), f1=(200.0, 300.0));
-c:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
-c:4: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

Alternative visualization

In [22]:
import mpld3
mpld3.enable_notebook()
In [23]:
t = ts[:4000]
s = sig_gen(t, f0=220.0, f1=224.0)

plt.plot(t, s);
In [24]:
npts = 50
rnd_err = np.random.randn(npts)*15

# simulate quadratic process
a, b, c = 0.5, -2.4, -8.0
x = np.linspace(-10, 20, npts)
y = a * x**2 + b * x + c

dat = y + rnd_err

# fit
p = np.polyfit(x, dat, 2)
y_fit = np.polyval(p, x)

#plt.plot(x, dat, 'ko', mfc='none');
plt.plot(x, dat, 'ko');
plt.plot(x, y, 'g');
plt.plot(x, y_fit, 'r');

seaborn

In [25]:
mpld3.disable_notebook()
npts = 500
x1 = np.random.normal(-1, 1, size=npts)
x2 = np.random.normal(1, 1, size=npts)
x3 = np.random.normal(2, 1, size=npts)

plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);

matplotlib is a good plotting library. It can be used to create beautiful visualizations, but not with the default plotting style.

In [26]:
# better default plotting style
import seaborn as sb

plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);
In [27]:
# works with d3
mpld3.enable_notebook()

plt.hist(x1, normed=True);
plt.hist(x2, normed=True);
plt.hist(x3, normed=True);
In [28]:
# lot's of styles and helper plots
sb.set_style('whitegrid')

sb.set_palette("deep")
#sb.set_palette(sb.color_palette("Set2"))

nbins = 10
sb.distplot(x1, bins=nbins);
sb.distplot(x2, bins=nbins);
sb.distplot(x3, bins=nbins);
In [29]:
sb.palplot(sb.color_palette())
In [30]:
mpld3.disable_notebook()