To set up, use pylab
magic:
In [1]:
%pylab inline
In [2]:
p1 = range(1,9)
p2 = range(2,10)
print p1, p2
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]:
In [4]:
p1 = range(4,9)
p2 = range(5,8)
print p1, p2
In [5]:
x = [mean(p2)]*len(p2)
y = [mean(p1)]*len(p1)
print x, y
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]:
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
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]:
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]:
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]: