James had the suggestion that I should give an IPython notebook talk/tutorial. I thought of a few things I could mention:
Examples that do not need to see the editing
%load_ext autoreload%autoreload 1%aimport blahnbagg backendExamples that need to view the editing
Examples that need the command line
IPython notebook cannot be run from the default installation it seems, so perhaps an introduction to the Anaconda python distribution?
Usually the beginning of a notebook is the same. The following code sets up embedded matplotlib plots (saved as pngs):
In [75]:
%matplotlib inline
Increase the resolution of the pngs created (mostly for OSX retina screens):
In [76]:
%config InlineBackend.figure_format = 'retina' # or svg for vector plots
Some imports
In [77]:
import matplotlib
import IPython
In [78]:
import matplotlib.pyplot as plt
import numpy as np
from astropy.io import fits as pyfits
In [79]:
def reset_matplotlib():
matplotlib.rc_file_defaults()
ipython = IPython.get_ipython()
ipython.magic('matplotlib inline')
ipython.magic('config InlineBackend.figure_format = "retina"')
matplotlib.rc('figure', figsize=(8, 6))
This is what IPython is really good for
In [80]:
reset_matplotlib()
In [81]:
x = np.arange(10)
y = x ** 2
plt.plot(x, y)
Out[81]:
In [82]:
%matplotlib inline
This is some markdown. Let's look at this link, or this list:
and what about some $\LaTeX$?
$$a = \sum_i^N x_i^2$$This is some markdown. Let's look at [this link](http://example.com), or this list:
* a
* b
* c
and what about some $\LaTeX$?
$$a = \sum_i^N x_i^2$$
Cells have different modes:
Keyboard shortcuts
jk to change slideCommand mode shortcuts
%: line magic%%: cell magic
In [83]:
%%sh
pwd
whoami
uptime
uname -a
In [84]:
files = !ls
print(files) # A list!
print(len(files))
In [85]:
!find c-example -type f -delete
In [86]:
%%file c-example/main.c
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
In [87]:
%%file c-example/Makefile
all: a.out
a.out: main.c
gcc $< -o $@
In [88]:
!make -C c-example && ./c-example/a.out
Open an embedded console for interactive play, with %qtconsole:
In [89]:
%time a = 10
In [90]:
%timeit a = 10
In [91]:
%%timeit
a = np.arange(1024)
b = np.random.randint(50, size=1024)
c = a + b
Profile cell running with %prun
Run commands without brackets! (sometimes...)
In [92]:
%autocall 1
In [93]:
str 10
Out[93]:
In [94]:
%autocall 0
In [95]:
%%latex
$a = 10$
In [96]:
%%ruby
puts "Hello world"
In [97]:
%load_ext cythonmagic
In [98]:
%%cython
cdef int a = 10
cdef int b = 20
def f(int c):
return a * b * c
In [99]:
f(5)
Out[99]:
In [100]:
reset_matplotlib()
In [101]:
def plot_colour_example():
L = 6
x = np.linspace(0, L)
ncolors = len(plt.rcParams['axes.color_cycle'])
shift = np.linspace(0, L, 5, endpoint=False)
for s in shift:
plt.plot(x, np.sin(x + s), 'o-')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('title')
In [102]:
plot_colour_example()
In [103]:
print(plt.style.available)
plt.style.use('dark_background')
plot_colour_example()
In [104]:
reset_matplotlib()
Seaborn is a wrapper around matplotlib that improves the aesthetics. For example
In [105]:
reset_matplotlib()
In [106]:
# Set up some data
x = np.arange(10)
y = x ** 2
In [107]:
plt.plot(x, y)
Out[107]:
After seaborn:
In [108]:
import seaborn as sns
sns.set()
In [109]:
plt.plot(x, y)
Out[109]:
Nice distribution plotting
In [110]:
with sns.axes_style('white'):
x = np.random.normal(2., 5., size=1000)
y = np.random.exponential(5., size=1000)
sns.jointplot(x, y, kind='kde', stat_func=None)
In [111]:
from IPython.display import IFrame
seaborn_page = IFrame('http://web.stanford.edu/~mwaskom/software/seaborn/tutorial/color_palettes.html', width=700, height=450)
In [112]:
seaborn_page
Out[112]: