如何在不使用`plot3d_parametric_line`的情况下绘制参数曲线

时间:2019-04-18 09:55:33

标签: python matplotlib plot sympy

想法是绘制曲线:C(t) = (1 + cos(t))i + (1 + sin(t))j + (1 -sin(t)-cos(t))k。按照https://docs.sympy.org/latest/modules/plotting.html上绘图模块上的说明,可以使用plot3d_parametric_line来获取它:

方法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

尽管这是一种有效的方法,但还有另一种绘制方法而不使用plot3d_parametric_line而不是ax.plot来绘制它。我尝试过的:

方法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()

但是TypeError: object of type 'Add' has no len()出现了...

如何修复它,以便获得与方法1相同的曲线?

谢谢

1 个答案:

答案 0 :(得分:4)

在定义线性NumPy网格并计算x,y,z变量之后,您可以使用matplotlib中的3d绘图

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