Issue
I have x, y and z coordinates of a helical line like the blue line in the figure. The coordinates are 1D arrays. How can I convert this helical line to a helix surface like the one in the figure and plot it as a surface plot using matplotlib?
Solution
Assuming your helix is defined like
n = 100
z = np.linspace(0, 8*np.pi, n)
x = np.cos(z)
y = np.sin(z)
you can use plot_trisurf
from matplotlib
. First, you need to create outer and inner edge points. For example like this:
inner_radius = 0.6
outer_radius = 1.4
x_inner = np.cos(z)*inner_radius
x_outer = np.cos(z)*outer_radius
x_both = np.concatenate([x_inner, x_outer])
y_inner = np.sin(z)*inner_radius
y_outer = np.sin(z)*outer_radius
y_both = np.concatenate([y_inner, y_outer])
and then the triangles for triangulation
triangles = []
for i in range(0, n//2 - 1):
triangles.append([i, i + 1, n + i])
triangles.append([n + i + 1, n + i, i + 1])
And finally plot the surface
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection='3d')
ax.plot_trisurf(x_both, y_both, np.concatenate([z, z]), triangles=triangles)
plt.show()
Result:
Answered By - maciek97x
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.