Random Walk SurfaceΒΆ

../../_images/rand_walk_surf.png

Variation of a theme showing a (x,y,z,w) dataset converted to a 3-D surface.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm,colors,colormaps
import s3dlib.surface as s3d
import s3dlib.pntcloud as ptc
import csv as csv

# 1. Get data to examine ...........................................

def rand_line(length,seed):
    # Use Matplotlib 'animated 3D random walk' algorithm.
    np.random.seed(seed)
    line_data = np.empty((3, length))
    line_data[:, 0] = np.random.rand(3)
    for index in range(1, length):
        step = (np.random.rand(3) - 0.5) * 0.1
        line_data[:, index] = line_data[:, index - 1] + step
    return line_data.T  #note: transpose

N = 2000
verts = rand_line(N,6)
vals = np.arange(0,N,dtype=float)
data = np.column_stack((verts,vals))

# 2. Setup and map surface .........................................
pdg, drez, relden, cmap = 0.7, 2, 0.04, 'jet'
domain = [ [0,3], [0,2], [0,4] ]

line = s3d.SegmentLine(verts)
line.map_cmap_from_sequence(cmap)

cloudObj = ptc.Point3DCloud(drez,domain=domain)

#... define surface from point cloud of sample density ...
cloudObj.map_vals_from_sampdens(data,pdg)
denCld = str(cloudObj)
surface = cloudObj.valsurf(relden)

#... reset point cloud values from sample values ...
cloudObj.map_vals_from_sampvals(data)

#... set surface vertex values from cloud, then color map from values
surface.map_vertvals_from_cloud(cloudObj)
surface.map_cmap_from_vertvals(cmap)
surface.triangulate(1)

# 3. Construct figures, add surfaces, and plot ....................
fig = plt.figure(figsize=(10,4))
# ....................
title = 'sample density surface\n( pdg = {:.2f}, relden = {:.2f} )'.format(pdg,relden)
fig.text(.27,0.9,title,ha='center')
ax = fig.add_subplot(121, projection='3d', aspect='equal', focal_length=0.5)
ax.set(xlim=domain[0], ylim=domain[1], zlim=domain[2],  
    xlabel='X',ylabel='Y',zlabel='Z')
ax.view_init(10,-30 )
ax.add_collection3d(surface.shade(flat=False).hilite(.8,focus=2,flat=False))
s3d.add_boxCorner(ax,domain)
# ....................
title = 'random walk\n sample size: {}'.format(N)
fig.text(.7,0.9,title,ha='center')
ax = fig.add_subplot(122, projection='3d', aspect='equal', focal_length=0.5)
ax.set(xlim=domain[0], ylim=domain[1], zlim=domain[2],  
    xlabel='X',ylabel='Y',zlabel='Z')
ax.view_init(10,-30 )
s3d.add_boxCorner(ax,domain)
ax.add_collection3d(line.fade(ax=ax))
# ....................
scmp = cm.ScalarMappable(cmap=cmap)
cbar = plt.colorbar(scmp, ax=ax,  shrink=0.8, pad=0.15 )
cbar.set_label('sequence', rotation=270, labelpad=15)

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