This notebook collects some useful code snippets

Last edits:

  • 2013 Dec 12, MM: changed name and added to git
  • 2014 Jan 7, MM: color palettes (new), font tricks
  • 2014 July 22, MM: added output precision

Content

Font Tricks

Table of content


In [ ]:
# select any system font for the figure
import matplotlib.font_manager as fmt
FM = fmt.FontManager()
fnt = FM.findfont('Times New Roman')
fp = fmt.FontProperties(fname=fnt)
fp.set_size(9.0)

figure(figsize=(4,2))
plot(randn(1000))
gca().set_xticks(arange(10)*100)
gca().set_xticklabels(arange(10)*100, fontproperties=fp)
gca().set_yticks(arange(6) - 3)
gca().set_yticklabels(arange(6) - 3, fontproperties=fp)
gca().set_title('mein titel', fontproperties=fp)
#savefig('img/fonttest1.svg',)
#savefig('img/fonttest1.png')
#savefig('img/fonttest1.pdf')
#savefig('img/fonttest1.eps')
#savefig('img/fonttest1.ps')
pass

In [ ]:
# using the same font in mathematical expressions as elsewhere
import matplotlib.font_manager as fmt
FM = fmt.FontManager()
fnt = FM.findfont('Arial')
fp = fmt.FontProperties(fname=fnt)
fp.set_size(9.0)

import matplotlib as mpl
mpl.rcParams['mathtext.default'] = 'regular' # this is the key

figure(figsize=(5,2))
plot(randn(1000),'.')
gca().set_xticklabels(arange(10)*100, fontproperties=fp)

text(100, 0, r'This is a text with $x=y^2$ and $\alpha$', fontproperties=fp,
     bbox=dict(facecolor='#ffffff', edgecolor='blue', pad=10.0))

pass

In [ ]:
# manipulating the figure legend text

figure(figsize=(6,4))
plot(randn(1000),'bd', label='random data')

import matplotlib as mpl
mpl.rcParams['legend.fontsize'] = 9

import matplotlib.font_manager as fmt
FM = fmt.FontManager()
fnt = FM.findfont('Times New Roman')
fp = fmt.FontProperties(fname=fnt)
fp.set_size(9.0) # should not conflict with rcParams

leg = legend()
for label in leg.get_texts():
    label.set_fontproperties(fp)

Cython magics

table of content


In [ ]:
# prepare some test data
t = linspace(0,1,1000)
mydata = vstack([t-.5, sin(pi*t)]).T

# define a function in Python
def py_getforce(data,  k,  l0):
    out = zeros(data.shape[0],dtype=np.float64)
    for idx in range(data.shape[0]):
        out[idx] = k*(sqrt(data[idx,0]**2 + data[idx,1]**2) - l0)
    return out

In [ ]:


In [ ]:
%load_ext cythonmagic

In [ ]:
%%cython -lm

# This is the same function, just in Cython
import numpy as np
cimport numpy as np
from libc.math cimport sqrt # much, much faster than np.sqrt


def getforce(np.ndarray[np.float_t, ndim=2] data, double k, 
             double l0):
    cdef double uv  # unused variable
    out = np.zeros(data.shape[0],dtype=np.float64)
    for idx in range(data.shape[0]):
        out[idx] = k*(sqrt(data[idx,0]**2 + data[idx,1]**2) - l0)
    return out

In [ ]:
# run performance test
%timeit f = getforce(mydata,1000., 1.)
%timeit fp = py_getforce(mydata, 1000., 1.)

f = getforce(mydata,1000., 1.)
fp = py_getforce(mydata, 1000., 1.)
print "same results?", allclose(f,fp)

In [ ]:
import mutils.makeODE as mo
#print mo.ex_py_code # print the example code
# to print the C_code, do: print mo.ex_c_code
#exec mo.ex_py_code # run the example code
#print mo.ex_c_code
print mo.ex_c_code
exec mo.ex_py_code

In [ ]:
# example: 3D SLIP implementation including energy injection at midstance
import models.slip as sl

# parameter: k, l0, m, alpha, beta, energy change
param = [20000., 1, 80., 68.*pi/180, 0, 0]
# initial condition y, vx, vz
IC = [1, 5., 0]
t, state = sl.qSLIP_step3D(IC, param)

# visualize
figure()
plot(t, state[:,1]) # state is [x,y,z,vx,vy,vz]; plot y

# now loop for performance test
fs = state[-1, [1,3,5]] # the final apex state y, vx, vz
import time
t0 = time.time()

for step in range(1000):
    IC = fs
    t, state = sl.qSLIP_step3D(fs, param)
    fs = state[-1, [1,3,5]]

      
tf = time.time()
print "1000 steps in {:.2f} seconds".format(tf-t0)

User-defined color axes

table of content


In [ ]:
# for each color, create a list of segments:
# ( (x1, va1, vb1), (x2, va2, vb2), ... (xn, vn1, vn2))
# where x give the relative position on the color axis (Note: x1 = 0, xn = 1)
# and va, vb give the corresponding values of the color just before and just after that position
# if va = vb, the color axis is continous

from matplotlib.colors import LinearSegmentedColormap

# continous black-red-black
cdict_1 = {'red' : ( (0.0, 0.0, 0.0),
                     (0.5, 1.0, 1.0),
                     (1.0, 0.0, 0.0) ),
           'green' : ( (0.0, 0.0, 0.0),
                     (0.5, 0.0, 0.0),
                     (1.0, 0.0, 0.0) ),
           'blue' : ( (0.0, 0.0, 0.0),
                     (0.5, 0.0, 0.0),
                     (1.0, 0.0, 0.0) ),
           }
map_brb = LinearSegmentedColormap('BRB1', cdict_1)


# discontinous black-red-white-green
cdict_2 = {'red' : ( (0.0, 0.0, 0.0),
                     (0.5, 1.0, 1.0),
                     (1.0, 0.0, 0.0) ),
           'green' : ( (0.0, 0.0, 0.0),
                     (0.5, 0.0, 1.0),
                     (1.0, 1.0, 1.0) ),
           'blue' : ( (0.0, 0.0, 0.0),
                     (0.5, 0.0, 1.0),
                     (1.0, 0.0, 0.0) ),
           }
map_brwg = LinearSegmentedColormap('BRWG1', cdict_2)

figure()
data = arange(50)[newaxis, :]
subplot(2, 1, 1)
pcolor(data, cmap=map_brb)

subplot(2, 1, 2)
pcolor(data, cmap=map_brwg)

Sort a list

Sort list after arbitrary comparison function

table of content


In [11]:
mylist = [("name A", 12), ("string X", 3), ("ooopsi", 7)]
sorted_list = sorted(mylist, key=lambda elem: int(elem[1]))
print sorted_list


[('string X', 3), ('ooopsi', 7), ('name A', 12)]

Flatten a list

The key is to understand that list comprehensions are nested, and the latter parts correspond to the "more inner" for-loops of nested for-loops

table of content


In [ ]:
lst1 = ['a', 'b', 'c', 'd']
lst2 = [(x + '_x', x + '_y') for x in lst1]
print "lst2", lst2

# how to get this list "flat"?
# --- long code:
flat1 = []
for pair in lst2:
    for elem in pair:
        flat1.append(elem)
        
# --- short code:
flat2 = [elem for pair in lst2 for elem in pair]

# show results
print "flat1:", flat1
print "flat2:", flat2

Color palettes

table of content


In [ ]:
import matplotlib.patches as mpatches

x0 = array([0,0,.8,.8, 1]) # last value is "dummy" (ignored)
y0 = array([0,1,1,0, 1]) # last value is "dummy"
# T is a translation operator
T = vstack([hstack([eye(4),array([[1,1,1,1]]).T]),[0,0,0,0,1]])
# P is projector (removes dummy)
P = eye(4,5)
# convenience operation: applies a list of operators. usage
def mop(*args):
    """ usage: mop(operator1, ...., operatorN, vector) """
    return reduce(dot, args)

# guided by colorbrewers2.org
colors1 = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c', '#fb9a99', '#e31a1c',
          '#fdbf6f', '#ff7f00', '#cab2d6', '#6a3d9a']

figure(figsize=(8,2))
x = x0.copy()
for c in colors1:
    x = dot(T,x) # translate x
    fill(dot(P,x), dot(P,y0),c)

title('colorbrewers2.org')
gca().set_xticks([])
gca().set_yticks([])

# brightness adapted
colors2 = ['#000000', '#106aa4', '#43bf3c', '#ff7f00', '#ef0a28', '#5f2e82',
           '#8f8f8f', '#92bee3', '#a1df80', '#fdaf5f', '#fb8987', '#baa2c5',]

figure(figsize=(8,2))
x = x0.copy()
for c in colors2:
    x = dot(T,x) # translate x
    fill(dot(P,x), dot(P,y0),c)

title('adaptations by M.Maus')
gca().set_xticks([])
gca().set_yticks([])


pass # suppress output

Re-using figures

table of content


In [ ]:
# create figure
%config InlineBackend.close_figures = False
fig = figure()
plot(randn(100),'bd')

In [ ]:
# "call" the figure

# edit the figure
xlim(0,20)
fig # one way - "call" the figure
#gcf() # another way

pass

Axis tricks: ticks and labels

Switch off some axes, and put labels to arbitrary positions

table of content


In [ ]:
# sw
figure()
ax = subplot(111)
ax.plot(randn(100))
ax.spines['right'].set_visible(False)
ax.tick_params(labelleft='off', left='off', right='off' ) # don't put ticks and labels left, no ticks right

ax.set_xlabel('x-label', ha='left')
ax.set_ylabel('y-label')
# position labels
ax.get_yaxis().set_label_coords(-0.08, 0.9) # (x,y) from 0 to 1
ax.get_xaxis().set_label_coords(0, -.1) # (x,y)

# do it manually for the title
tt = ax.set_title('title', ha='right')
tt.set_position((1, 1))

Run cells from (other) notebooks

table of content


In [ ]:
# prepare the test cell: It must contain a comment with the keyword "cell_ID" and the identifier (string)
# cell_ID testcell
# note: for cell_ids only dot(.), upper- and lowercase letters and numbers are currently supported
print "This is test cell"

In [ ]:
import mutils.misc as mi
nbfile="snippets.ipynb" # the name of the notebook; don't forget the extension!
mi.run_nbcells(nbfile, ['testcell',]) #run the selected cell(s)

In [ ]:
%Inl

3D plotting

table of content


In [ ]:
# --- 3D plotting
from mpl_toolkits.mplot3d.axes3d import Axes3D
fig = figure(figsize=(16 * .75,9 * .75))
ax1 = fig.add_axes([0,0,.8,1], projection='3d') #add_subplot(1,1,1,projection='3d')
ax2 = fig.add_axes([.8,0,.2,1], frameon=False)
ax2.set_xticks([])
ax2.set_yticks([])
x = linspace(0,20,100)
ax2.plot(x, sin(x), 'r')
ax2.plot(x, exp(sin(x)) + 2, 'g.-')
ax1.view_init(10,25)
l1 = ax1.plot3D(x, sin(x), cos(x), 'r.-')
l2 = ax1.plot3D(x, exp(sin(x)), -1 + .5*cos(x), 'g+-')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_zlabel('z')
draw()

animations

table of content

create the animation

(here, we use the ogg video format because it can be displayed in my favourite browsers ;) )


In [ ]:
# create the data: a point rotating on a circle
!mkdir -p 'tmp'
fig = figure()
ax = fig.add_subplot(1,1,1)
axis('equal')
phi = linspace(0, 2*pi, 100)
ax.plot(sin(phi), cos(phi),'b')
diam = ax.plot(sin(phi[0]), cos(phi[0]), 'rd')
for nr, t in enumerate(phi):
    diam[0].set_xdata(sin(t))
    diam[0].set_ydata(cos(t))
    draw()
    fig.savefig('tmp/_{:04d}.png'.format(nr))

In [ ]:
#convert into animation
!avconv -y -r 20 -f image2 -i tmp/_%04d.png  -vb 600k tmp/anim.ogg

# note: if the filenames do not begin with _0000.png but let's say with _0007.png, do this:
#$start_idx = 7
#!avconv -y -r 20 -start_number $start_idx -f image2 -i tmp/_%04d.png  -vb 600k tmp/anim.ogg

In [ ]:
# delete the data
!rm tmp/_*.png

embed the animation in IPython notebook


In [ ]:
# display the video inline!
# note: this needs a recent browser... with HTML5 and the option of OGG-video playback
from IPython.core.display import HTML
video = open('tmp/anim.ogg', 'rb').read()
video_encoded = video.encode('base64') # encode the video in HTML-conform style
video_tag = ('<video controls type="video/ogg" '
             + 'alt="VPP walking model" src="data:video/ogg;base64,{0}">').format(video_encoded)
HTML(data = video_tag)

In [ ]:

better pcolor (replacement)

table of content


In [ ]:
# That's easy: take imshow!
# further: imshow exports in pdf as bitmap!!


X, Y = meshgrid(arange(10)*.3, arange(10)*.2)
Z = X**2 + Y**2

figure(figsize=(16,5))

subplot(1,3,1)
pcolor(X,Y,Z)
title('pcolor')

subplot(1,3,2)
imshow(Z[::-1,:], extent=(X.min(), X.max(), Y.min(), Y.max()), aspect='auto')
title('imshow')

subplot(1,3,3)
imshow(Z[::-1,:], interpolation='none', extent=(X.min(), X.max(), Y.min(), Y.max()), aspect='auto')
title('imshow w/o interpolation')

In [ ]:
data

transparent ribbon around a line

table of content


In [8]:
def plotband(x, y, ups, downs, color='#0067ea', alpha=.25, **kwargs):
    """
    plots a line surrounded by a ribbon of the same color, but semi-transparent
    
    :args:
        x (N-by-1): positions on horizontal axis
        y (N-by-1): corresponding vertical values of the (center) line
        ups (N-by-1): upper edge of the ribbon
        downs (N-by-1): lower edge of the ribbon
        
    :returns:
        [line, patch] returns of underlying "plot" and "fill_between" function
    """
    pt1 = plot(x, y, color, **kwargs )
    pt2 = fill_between(x, ups, downs, color='None', facecolor=color, lw=0, alpha=alpha)
    return [pt1, pt2]

# how to use:
xdat = arange(10)
ydat = xdat**2
ups = ydat + xdat
downs = ydat - 3
figure()
plotband(xdat, ydat, ups, downs, color='#997444', linestyle='-.', lw=2)
pass


Set output precision for floats and arrays

table of content


In [6]:
%precision 4
pi   # note: "print pi" yields "3.141592..." because that's the string representation of a float (pi.__str__() is called) !


Out[6]:
3.1416

In [7]:
print randn(10)
print pi  # <- this is a float!
print array([pi])


[-0.5924 -0.252  -0.3975  0.4454  0.5696 -1.1843 -0.6125  1.5249  0.8172
 -0.0184]
3.14159265359
[ 3.1416]

Interactive plotting with "Bokeh"

table of content

NOTE:

This requires the "bokeh" package to be installed. If not installed, you can install it by this shell command:

sudo pip install bokeh


In [ ]:
# example copied from bokeh introduction. For a documentation, click on the "bokeh" symbol!

from bokeh.plotting import *
output_notebook() # connect to notebook!

In [ ]:


In [ ]:
# Skip the first point because it can be troublesome
theta = linspace(0, 8*np.pi, 10000)[1:]

figure()
hold()
# Compute the radial coordinates for some different spirals
lituus = theta**(-1./2.)          # lituus
golden = exp(0.306349*theta) # golden
arch   = theta                  # Archimedean
fermat = theta**(1./2.)           # Fermat's

# Now compute the X and Y coordinates (polar mappers planned for Bokeh later)
golden_x = golden*cos(theta)
golden_y = golden*sin(theta)
lituus_x = lituus*cos(theta)
lituus_y = lituus*sin(theta)
arch_x   = arch*cos(theta)
arch_y   = arch*sin(theta)
fermat_x = fermat*cos(theta)
fermat_y = fermat*sin(theta)
line(arch_x, arch_y, color="red", line_width=2,
     title="Archimean", legend="Archimedean")
line(golden_x, golden_y, color="#ffeeaa", line_width=2, legend="Golden")
show()

Code genernation in C / Python / Fortan for Sympy expressions

table of content


In [13]:
import sympy as sp
import sympy.matrices as ma

# create some long matrix expressions
q,p = sp.symbols('q p')
expr = sp.Matrix([[1., sp.sin(q)],[sp.cos(q), 2.*p*q]])
expr2 = expr * expr * expr * expr * expr

In [14]:
# generate C-code. NOTE: this works only element-wise for a matrix!
from sympy.utilities.codegen import codegen
[(c_name, c_code), (h_name, c_header)] = codegen(
     ("J11", expr2[0,0]), "C", "test", header=False, empty=True)
print "/*File: {}\n----------------\n*/\n".format(c_name)
print c_code

# to just "print" the C-code, this suffices
#import sympy.printing as spr
#spr.ccode(expr2[0,0])


/*File: test.c
----------------
*/

#include "test.h"
#include <math.h>

double J11(double p, double q) {

   return (2.0*p*q*(2.0*p*q*(2.0*p*q*sin(q) + 1.0*sin(q)) + (sin(q)*cos(q) + 1.0)*sin(q)) + ((2.0*p*q*sin(q) + 1.0*sin(q))*cos(q) + 1.0*sin(q)*cos(q) + 1.0)*sin(q))*cos(q) + 1.0*(2.0*p*q*(2.0*p*q*sin(q) + 1.0*sin(q)) + (sin(q)*cos(q) + 1.0)*sin(q))*cos(q) + 1.0*(2.0*p*q*sin(q) + 1.0*sin(q))*cos(q) + 1.0*sin(q)*cos(q) + 1.0;

}


In [15]:
# compare performance

# create python and fortran functions
import sympy.utilities.autowrap as aw # for fortran code
from sympy.utilities.lambdify import lambdify # for python code

f1 = lambdify((p,q), expr2[0,0]) # python (actually, this CAN take expr2 and will return a numpy matrix)
f2 = aw.autowrap(expr2[0,0]) # fortran
# compare that all expressions evaluate to the same value
print expr2.subs(p,1).subs(q,2)[0,0].evalf()
print f1(1,2)
print f2(1,2)


-39.8058771907571
-39.8058771908
-39.8058771908

In [16]:
# run performance tests
%timeit expr2.subs(p,1).subs(q,2)[0,0].evalf()
%timeit f1(1,2)
%timeit f2(1,2)


100 loops, best of 3: 6.73 ms per loop
100000 loops, best of 3: 3.34 µs per loop
10000000 loops, best of 3: 172 ns per loop

In [ ]: