ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 05 Jan 2016 08:09:38 -0600How to embed a 3D matplotlib figure in the notebook interface?http://ask.sagemath.org/question/32079/how-to-embed-a-3d-matplotlib-figure-in-the-notebook-interface/I'd like to embed a matplotlib figure in the notebook interface of SageMath, is this possible? Presently I am using this SageMath code:
x,y,z=var('x,y,z')
# Next we define the parameters
sigma=10
rho=40
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=150
h=tmax/N
times=srange(0,tmax+h,h)
ics=[0,1,1]
sol=desolve_odeint(lorenz,ics,times,[x,y,z],rtol=1e-13,atol=1e-14)
X=sol[:,0]
Y=sol[:,1]
Z=sol[:,2]
# Plot the result
from mpl_toolkits.mplot3d import axes3d
from matplotlib import pyplot as plt
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()
to solve and plot the solution to the Lorenz equations. What modification to the `plot()` function do I have to make in order to embed the matplotlib plot in the notebook interface. Tue, 05 Jan 2016 02:51:50 -0600http://ask.sagemath.org/question/32079/how-to-embed-a-3d-matplotlib-figure-in-the-notebook-interface/Answer by kcrisman for <p>I'd like to embed a matplotlib figure in the notebook interface of SageMath, is this possible? Presently I am using this SageMath code:</p>
<pre><code>x,y,z=var('x,y,z')
# Next we define the parameters
sigma=10
rho=40
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=150
h=tmax/N
times=srange(0,tmax+h,h)
ics=[0,1,1]
sol=desolve_odeint(lorenz,ics,times,[x,y,z],rtol=1e-13,atol=1e-14)
X=sol[:,0]
Y=sol[:,1]
Z=sol[:,2]
# Plot the result
from mpl_toolkits.mplot3d import axes3d
from matplotlib import pyplot as plt
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()
</code></pre>
<p>to solve and plot the solution to the Lorenz equations. What modification to the <code>plot()</code> function do I have to make in order to embed the matplotlib plot in the notebook interface. </p>
http://ask.sagemath.org/question/32079/how-to-embed-a-3d-matplotlib-figure-in-the-notebook-interface/?answer=32087#post-id-32087First off, you probably don't need a separate function for that, and secondly you definitely shouldn't clobber the normal Sage `plot()` function with yours - maybe name it `plot1()`?
But the real thing you need is to create a file for sagenb to display, with something like
plt.savefig('test.png')
It would be nice to use the temporary filename creation capabilities in Sage but those create filenames in the whole tree; you could do something more annoying like
plt.savefig(os.path.basename(tmp_filename(ext='.png')))
Anyway, I get a nice Lorenz plot so I guess everything works okay! Nice code. I cannot say if this would work with other savefig outputs though as I don't use mpl directly.Tue, 05 Jan 2016 08:09:38 -0600http://ask.sagemath.org/question/32079/how-to-embed-a-3d-matplotlib-figure-in-the-notebook-interface/?answer=32087#post-id-32087