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 default direction of [0,0,1] is used for three separate purposes.

    1. To apply the colormap direction onto the surface (line 20).
    2. To set the orientation normal to the contour planes (line 22). The contours will take the color of the surface.
    3. To collapse the colored contours on a plane with normal to the direction (line 23).
  • 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 31)

 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
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)

# 2. Setup and map surface .........................................
rez,offset = 6, -5

plane = s3d.PlanarSurface(rez).domain( 2.25, (-2.5, 1.75) )
plane.map_geom_from_op(townsend)
plane.map_cmap_from_op()            # default: z-direction

contours = plane.contourLineSet(20) # default: planar, z-direction
contours.map_to_plane(offset)       # default: planar, z-direction
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', ylabel='Y')
s3d.auto_scale(ax,plane,contours)

ax.add_collection3d(plane.shade(0.5).hilite(.5))
ax.add_collection3d(contours)

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