Contour ProjectionsΒΆ

The Townsend function is given on the Wikipedia page Test functions for constrained optimization.

../../_images/townsend1.png

Several construction techniques are exemplified in this plot.

  • The geometric function, townsend, is not scaled, but instead, the surface domain is changed using the surface domain method (line 21)

  • The same direction is used for three separate purposes.

    1. To apply the colormap direction onto the surface (line 23).
    2. To set the orientation normal to the contour planes (line 25). The contours will take the color of the surface.
    3. To collapse the colored contours on a plane with normal to the direction (line 26).
  • Surface is then shaded and highlighted when added to the axes (line 36). This occurs after contour construction so the contour colors are not changed.

  • The axes are auto scaled based on the domains of the two objects added to the axes. (line 38)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
import matplotlib.pyplot as plt
import s3dlib.surface as s3d

#.. Function XY Surface Contours, XY Projection with Z direction surface colormapping.

# 1. Define function to examine ....................................

def townsend(xyz) :
    x,y,z = xyz
    A = -np.cos( (x-0.1)*y )**2
    B = -np.sin(3*x + y)
    return x, y, (A + x*B)

def colormap_direction(xyz,D) : return np.dot(xyz.T,D)

# 2. Setup and map surface .........................................
rez=6
direction,offset = [0,0,1], -5

plane = s3d.PlanarSurface(rez).domain( 2.25, (-2.5, 1.75) )
plane.map_geom_from_op(townsend)
plane.map_cmap_from_op( lambda xyz : colormap_direction(xyz,direction))

contours = plane.contourLineSet(20)
contours.map_to_plane(offset)
contours.set_linewidth(1)

# 3. Construct figure, add surface plot ............................

fig = plt.figure()
ax = plt.axes(projection='3d', proj_type='ortho')
ax.set_xlabel('X')
ax.set_ylabel('Y')

ax.add_collection3d(plane.shade(0.5).hilite(.5))
ax.add_collection3d(contours)
s3d.auto_scale(ax,plane,contours)

fig.tight_layout(pad=0)
plt.show()