简体   繁体   English

使用Sympy在3D中绘制曲线

[英]Plot a curve in 3D with Sympy

How to plot the following 3D curve (as an example) using Sympy? 如何使用Sympy绘制以下3D曲线(作为示例)? I know just create an array for t and do this in Matplotlib, but I don't really want to plot this curve, rather to learn how to define and plot curves symbolically. 我知道只需为t创建一个数组并在Matplotlib中执行此操作,但我不想绘制曲线,而是学习如何以符号方式定义和绘制曲线。

alpha(t) = (cos(t), sin(t), t) alpha(t)=(cos(t),sin(t),t)

from sympy import *

t = symbols('t')
alpha = [cos(t), sin(t), t]

# now what?

I have tried using the plot method in various ways but this has only resulted in either three separate 1D curves, or an error. 我尝试过以各种方式使用绘图方法,但这只会导致三条单独的1D曲线或错误。

You have to use methods from sympy.plotting , in your case your need plot3d_parametric_line : 您必须使用sympy.plotting中的方法,在您的情况下需要plot3d_parametric_line

from sympy import *
from sympy.plotting import plot3d_parametric_line

t = symbols('t')
alpha = [cos(t), sin(t), t]
plot3d_parametric_line(*alpha)

or plot3d_parametric_line(cos(t), sin(t), t, (t, 0, 2*pi)) if you like to set limits of t. plot3d_parametric_line(cos(t), sin(t), t, (t, 0, 2*pi))如果你想设置t的限制。

在此输入图像描述 Look for more examples to plot in 3d with sympy: https://github.com/sympy/sympy/blob/master/examples/beginner/plot_examples.py 寻找更多的例子在3d中用sympy绘制: https//github.com/sympy/sympy/blob/master/examples/beginner/plot_examples.py

I never tried ploting in sympy 我从未尝试过同情的情节

I suspect more will have matplotlib experience 我怀疑更多将有matplotlib经验

lambdify() is the interface between symbolic expressions and regular functions lambdify()是符号表达式和常规函数之间的接口

from sympy import *
from sympy.utilities.lambdify import lambdify
import math

t = symbols('t')
alpha = [cos(t), sin(t), t]

f = lambdify(t, alpha)

T = [2*math.pi/100*n for n in range(100)]
F = [f(x) for x in T]

import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

fig1, ax1 = plt.subplots(subplot_kw=dict(projection='3d'))
ax1.plot(*zip(*F))
ax1.set_aspect('equal')
plt.show()

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM