想法是绘制曲线: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
获取它:
方法一:
%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))
尽管这是一种有效的方法,但还有另一种不使用 plot3d_parametric_line
而使用 ax.plot
来绘制它的方法。我尝试过的:
方法二:
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()
comes...
我怎样才能修复它,以便获得与方法 1 相同的曲线?
谢谢
最佳答案
在定义线性 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()
关于python - 如何在不使用 `plot3d_parametric_line` 的情况下绘制参数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55743551/