Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How to plot the following 3D curve (as an example) using Sympy? 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.

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.

You have to use methods from sympy.plotting, in your case your need 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.

Look for more examples to plot in 3d with sympy: https://github.com/sympy/sympy/blob/master/examples/beginner/plot_examples.py

I suspect more will have matplotlib experience

lambdify() is the interface between symbolic expressions and regular functions

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()
                Sadly, this answer is outdated. This definition of alpha leads to an error AttributeError: 'Zero' object has no attribute 'cos'. With alpha = [2*t, 3*t, t], the second last line leads to NotImplementedError: It is not currently possible to manually set the aspect on 3D axes, which is described here.
– claudio
                Jun 28, 2020 at 6:57
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.