2

Win 10 x64 Anaconda Python 2.7

I'm plotting an involute spiral onto a Gaussian surface with the following code..

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Spiral parameters
samNum = 1000
spConst = 10.0
t = np.linspace(0, 6*np.pi, samNum)

# Coordinates of involute spiral on xy-plane
coords = np.zeros([samNum, 3])
coords[:,0] = spConst * (np.cos(t) + t * np.sin(t)) # x coord
coords[:,1] = spConst * (np.sin(t) - t * np.cos(t)) # y coord

# Paramters for 2D Gaussian surface 
amp = 200
sigma_x = 75.0
sigma_y = 75.0
theta = np.pi
a = np.cos(theta)**2 / (2 * sigma_x**2) + np.sin(theta)**2 / (2 * sigma_y**2)
b = -np.sin(2 * theta) / (4 * sigma_x**2) + np.sin(2 * theta) / (4 * sigma_y**2)
c = np.sin(theta)**2 / (2 * sigma_x**2) + np.cos(theta)**2 / (2 * sigma_y**2)

# z coords of spiral projected onto Gaussian surface
coords[:,2] = amp * np.exp(-(a * coords[:,0]**2 - 2 * b * coords[:,0]*coords[:,1] + c * coords[:,1]**2)) # z coord

# plot 3D spiral
ax.scatter(coords[:,0], coords[:,1], coords[:,2],  s=1, c='k')

# plot lines projecting 3D spiral on to the xy-plane
for p in range(samNum):  
    ax.plot([coords[p,0], coords[p,0]], [coords[p,1], coords[p,1]], [0, coords[p,2]], color='g', linewidth=0.1)

ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

This gives the following output...

enter image description here

I would like to convert the green ribbon into a continuous surface. I have had a look at parametric surfaces in matplotlib but cant get my head around how to covert this into a surface.

So is this possible? Any pointers appreciated.

1 Answer 1

3

In principle you have everything you need already there,

t = np.linspace(0, 6*np.pi, samNum)

T, Z = np.meshgrid(t, [0,1])
X = spConst * (np.cos(T) + T* np.sin(T))
Y = spConst * (np.sin(T) - T * np.cos(T))

gives you the X and Y coordinates, and the upper Z coordinate is obtained via Z[1,:] = coords[:,2].

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Spiral parameters
samNum = 1000
spConst = 10.0
t = np.linspace(0, 6*np.pi, samNum)

T, Z = np.meshgrid(t, [0,1])
X = spConst * (np.cos(T) + T* np.sin(T))
Y = spConst * (np.sin(T) - T * np.cos(T))

# Coordinates of involute spiral on xy-plane
coords = np.zeros([samNum, 3])
coords[:,0] = spConst * (np.cos(t) + t * np.sin(t)) # x coord
coords[:,1] = spConst * (np.sin(t) - t * np.cos(t)) # y coord

# Paramters for 2D Gaussian surface 
amp = 200
sigma_x = 75.0
sigma_y = 75.0
theta = np.pi
a = np.cos(theta)**2 / (2 * sigma_x**2) + np.sin(theta)**2 / (2 * sigma_y**2)
b = -np.sin(2 * theta) / (4 * sigma_x**2) + np.sin(2 * theta) / (4 * sigma_y**2)
c = np.sin(theta)**2 / (2 * sigma_x**2) + np.cos(theta)**2 / (2 * sigma_y**2)

# z coords of spiral projected onto Gaussian surface
coords[:,2] = amp * np.exp(-(a * coords[:,0]**2 - 2 * b * coords[:,0]*coords[:,1] + c * coords[:,1]**2)) # z coord

Z[1,:] = coords[:,2] 
ax.plot_surface(X,Y,Z)

# plot 3D spiral
ax.scatter(coords[:,0], coords[:,1], coords[:,2],  s=1, c='k')


ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I think its meshgrid I'm struggling with. What am I looking at when I'm looking at X, Y & Z? I can see that X[0,:]=X[1,:] (same with Y) but cant get my head around why do that? What are these meshgrid objects (no I cant make head nor tail our of the meshgrid docs). Am I making sense?
Is this a problem of understanding how meshgrid works or a problem of why the return of meshgrid is ideally suited to produce the kind of plot we're having here? (In any case I feel like I cannot give a good answer to that in the comments section of a different question.)
its both to be honest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.