Figures

IPython Notebooks are so much fun, I thought I'd use one to quickly make the figures for my presentation.

To set up, use pylab magic:


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

Vector parameter study

Define values for parameters one ($p_1$) and two ($p_2$), then visualize the parameter space.


In [2]:
p1 = range(1,9)
p2 = range(2,10)
print p1, p2


[1, 2, 3, 4, 5, 6, 7, 8] [2, 3, 4, 5, 6, 7, 8, 9]

In [3]:
plot(p1, p2, 'ro')
xlim((0, 10))
ylim((0, 10))
xticks([])
yticks([])
xlabel('$p_1$')
ylabel('$p_2$')
title('Vector parameter study')


Out[3]:
<matplotlib.text.Text at 0x106a51d10>

Centered parameter study

Define values for parameters one ($p_1$) and two ($p_2$), then visualize the parameter space.


In [4]:
p1 = range(4,9)
p2 = range(5,8)
print p1, p2


[4, 5, 6, 7, 8] [5, 6, 7]

In [5]:
x = [mean(p2)]*len(p2)
y = [mean(p1)]*len(p1)
print x, y


[6.0, 6.0, 6.0] [6.0, 6.0, 6.0, 6.0, 6.0]

In [6]:
plot(x, p2, 'ro')
plot(p1, y, 'ro')
xlim((0, 10))
ylim((0, 10))
xticks([])
yticks([])
xlabel('$p_1$')
ylabel('$p_2$')
title('Centered parameter study')


Out[6]:
<matplotlib.text.Text at 0x106f05bd0>

Multidim parameter study

Define values for parameters one ($p_1$) and two ($p_2$), then visualize the parameter space.


In [7]:
n_rows = 5
n_cols = 6
offset = 2
p1 = [i % n_cols + offset for i in range(n_rows*n_cols)]
p2 = [i / n_cols + offset for i in range(n_rows*n_cols)]
print p1, p2


[2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7] [2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6]

In [8]:
plot(p1, p2, 'ro')
xlim((0, 10))
ylim((0, 10))
xticks([])
yticks([])
xlabel('$p_1$')
ylabel('$p_2$')
title('Multidim parameter study')


Out[8]:
<matplotlib.text.Text at 0x106f6b510>

The Rosenbrock function

The Rosenbrock function is a standard test problem for optimization algorithms.

Start by defining a grid of values on the interval [-2, 2]:


In [9]:
n = 51
x1 = numpy.linspace(-2, 2, n)
x2 = x1.copy()
x1v, x2v = numpy.meshgrid(x1, x2)

Compute the values of the Rosenbrock function at the nodes of the grid:


In [10]:
rosenbrock = 100.0*(x2v - x1v**2)**2 + (1 - x1v)**2

Visualize the result as a filled contour plot:


In [11]:
contourf(x1v, x2v, rosenbrock, 50, cmap=cm.RdYlBu_r)
xticks([-2, 0, 2])
yticks([-2, 0, 2])
xlabel('$x_1$')
ylabel('$x_2$')


Out[11]:
<matplotlib.text.Text at 0x107045f90>

And as a surface plot:


In [12]:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x1v, x2v, rosenbrock, rstride=2, cstride=2, linewidth=0, cmap=cm.RdYlBu_r)
ax.view_init(elev=30, azim=45)
xticks([-2, 0, 2])
yticks([-2, 0, 2])
ax.set_zticks([0, 2000, 4000])
xlabel('$x_1$')
ylabel('$x_2$')
ax.set_zlabel('$f(x_1,x_2)$')


Out[12]:
<matplotlib.text.Text at 0x1074557d0>