Complex Cylindrical ContoursΒΆ

../../_images/complex_contours1.png

This script originated from a direct copy of the surface construction Complex Number Representation, Geometry and Colormap example. Unnecessary lines were commented out. The highlighted lines show the contours created directly from these surfaces. The default contour line color is the surface color from which the contour is constructed.

Similar to the Elevation Contours of a Datagrid example, a black background was placed over the grid to enhance the color contrast (blackGB function ).

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.lines as mlines
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import s3dlib.surface as s3d
import s3dlib.cmap_utilities as cmu

#.. Complex Number Representation, Geometry and Colormap: Polar Coordinates
#   note: copy from surface representation for contour construction.
# 1. Define functions to examine ....................................

real = True
imaginary =  not real

def sqrt_Z(rtz, isReal) :
    r,t,z = rtz
    T=2*t
    if isReal :  Z = np.sqrt(r)*np.cos(T/2)
    else :       Z = np.sqrt(r)*np.sin(T/2)
    return r,T,Z
    
def square_Z(rtz, isReal) :
    r,t,z = rtz
    if isReal : Z = r*r*np.cos(2*t)
    else :      Z = r*r*np.sin(2*t)
    return r,t,Z

def blackGB(rot=True):
    v = [ [1.0,-1.0,-1.0],[1.0,1.0,-1.0],[-1.0,1.0,-1.0],[-1.0,-1.0,-1.0],
          [1.0,-1.0,1.0],[1.0,1.0,1.0],[-1.0,1.0,1.0],[-1.0,-1.0,1.0] ]
    f = [ [0,1,2,3], [1,5,6,2], [2,6,7,3]]
    e = [ [0,1],[1,2],[2,3],[3,0],[1,5],[5,6],[6,7],[7,3],[2,6] ]
    bgobj =  s3d.Surface3DCollection(v,f,e,color='k',edgecolor='dimgrey')
    if rot : bgobj.transform( s3d.axisRot(-90.0,[0,0,1] ))
    return bgobj

# 2. Setup and map surfaces .........................................

zMap = cmu.hsv_cmap_gradient( [.5,1,1], [1.5,1,1] )
bmap = cmu.binary_cmap()

surface_1 = s3d.PolarSurface(4, cmap=zMap,linewidth=0.1)
surface_1.map_cmap_from_op( lambda rtz : sqrt_Z(rtz,imaginary)[2] )
surface_1.map_geom_from_op( lambda rtz : sqrt_Z(rtz,real) )
#surface_1.set_edgecolor([0,0,0])

surface_I = s3d.PolarSurface(5, facecolor='red')
surface_I.map_geom_from_op( lambda rtz : square_Z(rtz,imaginary) )
surface_R = s3d.PolarSurface(5, facecolor='blue')
surface_R.map_geom_from_op( lambda rtz : square_Z(rtz,real) )

#surface_2 = surface_I + surface_R
#surface_2.shade(direction=[1,1,1])

# ..... construct contours from surfaces ......

line_1 = surface_1.contourLineSet(20,coor='c',name='real sqrt contours')

line_I = surface_I.contourLineSet(20,coor='c',name='imag sqar contours')
line_R = surface_R.contourLineSet(20,coor='c',name='real sqar contours')

line_2 = line_I + line_R

# 3. Construct figure, add surfaces, and plot .....................

fcc = r'     f: $\mathrm{\mathbb{C}}$ $\to$  $\mathrm{\mathbb{C}}$' +'\n\n'
minmax, ticks = (-1,1) ,  [-1,0,1]
fig = plt.figure(figsize=plt.figaspect(0.6/1.2))

ax1 = fig.add_subplot(121, projection='3d')
ax1.view_init(20, 205)
ax1.set(xlim=minmax, ylim=minmax, zlim=minmax)
ax1.set_xticks(ticks)
ax1.set_yticks(ticks)
ax1.set_zticks(ticks)
ax1.set_xlabel('X axis')
ax1.set_ylabel('Y axis')
ax1.set_title( r'f(z) = $\sqrt{z}$' + fcc )
cbar = plt.colorbar(surface_1.cBar_ScalarMappable, ax=ax1,  shrink=0.6, pad=-.05 )
cbar.set_label('Imaginary', rotation=270, labelpad = 15)
cbar.ax.yaxis.set_major_formatter(FormatStrFormatter('%5.2f'))
real_handle = mlines.Line2D([], [], color='grey', marker='+', linestyle='None',
                          markersize=7, label='Real')
ax1.legend(handles=[real_handle])

ax2 = fig.add_subplot(122, projection='3d')
ax2.set(xlim=minmax, ylim=minmax, zlim=minmax)
ax2.set_xticks(ticks)
ax2.set_yticks(ticks)
ax2.set_zticks(ticks)
ax2.set_xlabel('X axis')
ax2.set_ylabel('Y axis')
ax2.set_title( r'f(z) =  $z^2$' + fcc )
red_patch = mpatches.Patch(color='red', label='Img')
blue_patch = mpatches.Patch(color='blue', label='Real')
ax2.legend(handles=[red_patch,blue_patch])

#ax1.add_collection3d(surface_1)
#ax2.add_collection3d(surface_2)
ax1.add_collection3d(blackGB())
ax1.add_collection3d(line_1)
ax2.add_collection3d(blackGB(False))
ax2.add_collection3d(line_2.fade())


fig.tight_layout()
plt.show()