JD_PM
JD_PM

Reputation: 159

How to plot a parametric curve without using `plot3d_parametric_line`

The idea is to plot the curve: C(t) = (1 + cos(t))i + (1 + sin(t))j + (1 -sin(t)-cos(t))k. Following the instructions on the Plot Module at https://docs.sympy.org/latest/modules/plotting.html one can get it using plot3d_parametric_line:

Method 1:

%matplotlib notebook
from sympy import cos, sin
from sympy.plotting import plot3d_parametric_line
t = sp.symbols('t',real=True)
plot3d_parametric_line(1 + cos(t), 1 + sin(t), 1-sin(t)-cos(t), (t, 0, 2*sp.pi))

enter image description here

Though this is a valid method there is another way to plot it without using plot3d_parametric_line but ax.plot. What I have tried:

Method 2:

fig = plt.figure(figsize=(8, 6))
ax = fig.gca(projection='3d')
ax.set_xlim([-0.15, 2.25])
ax.set_ylim([-0.15, 2.25])
ax.set_zlim([-0.75, 2.50])

ax.plot(1+sp.cos(t),1+sp.sin(t),1-sp.sin(t)-sp.cos(t))
plt.show()

But TypeError: object of type 'Add' has no len() comes up...

How can I fix it so that I get the same curve than with method 1?

Thanks

Upvotes: 3

Views: 1301

Answers (1)

Sheldore
Sheldore

Reputation: 39052

You can use the 3d plotting from matplotlib after defining a linear NumPy mesh and computing your x, y, z variables

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

fig = plt.figure()
ax = fig.gca(projection='3d')

t = np.linspace(0, 2*np.pi, 100)
x = 1 + np.cos(t)
y = 1 + np.sin(t)
z = 1 - np.sin(t) - np.cos(t)

ax.plot(x, y, z)

plt.show()

enter image description here

Upvotes: 4

Related Questions