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

标签 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 获取它:

方法一:

%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 来绘制它的方法。我尝试过的:

方法二:

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

enter image description here

关于python - 如何在不使用 `plot3d_parametric_line` 的情况下绘制参数曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55743551/

相关文章:

python - 索引错误: list index out of range - when plotting images

r - 在 R : how to highlight specific areas of interest? 中抛光蜘蛛网图

python - 参数维度不兼容,filling_ Between 出现问题

python - 如何在Python中的Selenium中截取相同窗口大小的屏幕截图?

function - 绘制上对数轴作为下线性轴的函数

python - 约束不匹配错误

plot - 对 VegaLite map 的边缘设置限制

python - scipy:基本说明

python - 你如何在 Numpy 中获得向量的大小?

来自 CSV 数据的 Python 饼图