Ask Your Question

# How do I plot a 3D Lorenz attractor with x, y and z labels?

I have been attempting to perform a 3D wireframe plot of the solution to the Lorenz equations, which is stored in the variables X, Y and Z. This is what I am presently using (unsuccessfully I might add):

 # Plot the result
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import axes3d

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
plt.figure(1)
plt.xlabel('X(t)')
plt.ylabel('Y(t)')
plt.zlabel('Z(t)')
axes3d.plot_wireframe(X,Y,Z)
plt.show()


How do I get this plot to work? I'm guessing you'll probably be able to guess that X, Y and Z are ndarrays, produced from this SageMath script:

x,y,z=var('x,y,z')

# Next we define the parameters
sigma=10
rho=28
beta=8/3

# The Lorenz equations
lorenz=[sigma*(y-x),x*(rho-z)-y,x*y-beta*z]

# Time and initial conditions
N=250000
tmax=250
h=tmax/N
t=srange(0,tmax+h,h)
ics=[0,1,1]
sol=desolve_odeint(lorenz,ics,t,[x,y,z],rtol=1e-13,atol=1e-14)
X=sol[:,0]
Y=sol[:,1]
Z=sol[:,2]

edit retag close merge delete

## Comments

1

Googling a bit, I found a nice blog post here

http://sagemath.blogspot.cl/2008/01/j...

( 2015-12-28 19:56:55 -0500 )edit

Thanks, you wouldn't happen to know where I can find detailed documentation on line3d would ya? I've Googled it but I can't find anything better than this, which is pretty minimal (only one mention to line3d in the entire document).

( 2015-12-29 01:05:08 -0500 )edit

## 2 answers

Sort by ยป oldest newest most voted

I have found the answer to this question (and it came out of this answer to a question of mine at StackOverflow) and it does, in fact, involve matplotlib. Here is the code required to plot the X, Y and Z ndarrays and provide the axes labels (the axes info I got from here):

# Plot the result
from mpl_toolkits.mplot3d import axes3d
from matplotlib import pyplot as plt
# Call the plot function if you want to plot the data
def plot():
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
ax.set_xlabel('X(t)')
ax.set_ylabel('Y(t)')
ax.set_zlabel('Z(t)')
plt.show()

plot()


which produces the result:

more

## Comments

While trying to run the same program I get the following error:"Arguement Z must be 2-dimensional." How do I change Z so that I can overcome this error?

( 2019-04-14 03:45:34 -0500 )edit

Unless you really need to use matplotlib, why not just use Sage's parametric_plot3d?

http://doc.sagemath.org/html/en/refer...

more

## Comments

Could you show me how to do this? I looked at the documentation and as I am not very experienced with SageMath it was not particularly helpful. I tried running parametric_plot3d((X,Y,Z),(t[0],t[N])) and it returned this error.

( 2015-12-28 03:49:42 -0500 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2015-12-26 07:50:53 -0500

Seen: 1,295 times

Last updated: Jan 02 '16