Cylindrical Contours

../../_images/twist_contours.png
import numpy as np
from matplotlib import pyplot as plt
import s3dlib.surface as s3d

#.. Cylindrical Surface Contours

# 1. Define functions to examine ....................................

def twistFunction(rtz,twists) :
    r,t,z = rtz
    # Note: sliced surface needed due to discontinuity @ t=0 if twists is odd
    thickness = 0.33
    w = thickness*z 
    phi = 0.5*t*twists
    R = 1 + w * np.cos(phi)
    Z = w * np.sin(phi)
    return R,t,Z

def ribbonFunc(rtz) :
    r,t,z = rtz
    min_radius, max_radius = 0.25, 0.95
    d = (max_radius-min_radius)/2
    R = d + min_radius + d*z
    Z = np.cos(R)*np.sin(3*t)
    return R,t,Z

def radius(xyz) :
    rtz = s3d.CylindricalSurface.coor_convert(xyz,False)
    return rtz[0]

# 2. Setup and map surfaces .........................................
rez = 5

twist = s3d.CylindricalSurface(rez, basetype='squ_s')
twist.map_geom_from_op( lambda rtz : twistFunction(rtz,1) )
tline = twist.contourLineSet(8, name='Twist Contours') # default: cylincrical contours
tline.map_cmap_from_op(radius,'Spectral').fade(.05)

ribbon = s3d.CylindricalSurface(rez, basetype='tri')
ribbon.map_geom_from_op( ribbonFunc )
rline = ribbon.contourLineSet(8, name='Ribbon Contours') # default: cylincrical contours
rline.map_cmap_from_op(radius,'CMRmap').fade(.05)

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

contours = [ tline, rline ]
minmax = (-.8,0.8)

fig = plt.figure(figsize=[10,4],facecolor='k')
fig.text(0.25,0.05,str(tline), ha='center', va='bottom', color='w',
    fontsize='smaller', multialignment='right')
fig.text(0.75,0.05,str(rline), ha='center', va='bottom', color='w',
    fontsize='smaller', multialignment='right')

for i in range(2) :
    ax = fig.add_subplot(121+i, projection='3d', facecolor='k', aspect='equal')
    ax.set(xlim=minmax, ylim=minmax, zlim=minmax )
    ax.set_axis_off()
    cSet = contours[i]
    ax.set_title(cSet.name,color='w')

    cbar = plt.colorbar(cSet.stcBar_ScalarMappable(8), ax=ax, shrink=0.6 )
    cbar.set_label(tline.cname, rotation=270, labelpad = 15, color='w')
    cbar.outline.set_edgecolor('dimgrey')
    plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='w')

    ax.add_collection3d(cSet)

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