0

I am trying to draw helix (shape of spring). I was able to draw a single helix using axes3D and matplotlib. Below is my code:

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

theta = np.linspace(-9 * np.pi, 9 * np.pi, 300)

radius = 5.0
x = radius*np.cos(theta)

x=[]
for i in theta:
    if (i < 4.5* np.pi):
        x.append(radius*np.cos(i))
    else:
        x.append((radius+2.0) * np.cos(i))

y=[]
for j in theta:
    if (j < 4.5* np.pi):
        y.append(radius*np.sin(j))
    else:
        y.append((radius+2.0) * np.sin(j))

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot(x, y, theta, 
        label = 'Parametric Curve', # label of the curve
        color = 'DarkMagenta',      # colour of the curve
        linewidth = 1,            # thickness of the line
        linestyle = '-'            # available styles - -- -. :
        )
rcParams['legend.fontsize'] = 11    # legend font size
ax.legend()                         # adds the legend



ax.set_xlabel('X axis')
ax.set_xlim(-5, 5)
ax.set_ylabel('Y axis')
ax.set_ylim(-10, 10)
ax.set_zlabel('Z axis')
ax.set_zlim(-9*np.pi, 9*np.pi)

ax.set_title('3D line plot,\n parametric curve', va='bottom')

plt.show()                                  # display the plot

I have two questions:

1) I was able to adjust the radius of my spiral but was not able to adjust the number of pitch. What changes should i make so I can have 19 circular rings, instead of 9.

2) After certain point(ie. end point of helix), I want to increase my radius and create a right-handed helix that goes all the way to bottom to the starting point of my first helix ( my first helix was left-handed helix). I was able to increase my radius but was not able to change the orientation of my helix and was not able to move it downwards.

After reading the documentation of matplotlib I could find: The example below illustrates a plotting several lines with different format styles in one command using arrays.

import numpy as np
import matplotlib.pyplot as plt

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show() 

Why cannot I do the same when there are three axes?

0 Answers0