{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Examples of 3D plots from Sage's reference manual\n",
"# rendered with three.js\n",
"\n",
"This notebook runs some examples of the [3D graphics section](http://doc.sagemath.org/html/en/reference/plot3d/) of Sage's reference manual with the [three.js](http://doc.sagemath.org/html/en/reference/plot3d/threejs.html) renderer."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'SageMath version 9.0.beta4, Release Date: 2019-11-02'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"version()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples from [*Plotting functions*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/plot3d.html)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x, y = var('x y')\n",
"W = plot3d(sin(pi*((x)^2+(y)^2))/2,(x,-1,1),(y,-1,1), frame=False, \n",
" color='purple', opacity=0.8)\n",
"S = sphere((0,0,0),size=0.3, color='red', aspect_ratio=[1,1,1])\n",
"show(W + S, viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def f(x,y):\n",
" return math.sin(y^2+x^2)/math.sqrt(x^2+y^2+0.0001)\n",
"P = plot3d(f,(-3,3),(-3,3), adaptive=True, color=rainbow(60, 'rgbtuple'), \n",
" max_bend=.1, max_depth=15)\n",
"P.show(viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"S = sphere(size=.5, color='yellow')\n",
"from sage.plot.plot3d.shapes import Cone\n",
"S += Cone(.5, .5, color='red').translate(0,0,.3)\n",
"S += sphere((.45,-.1,.15), size=.1, color='white') \\\n",
" + sphere((.51,-.1,.17), size=.05, color='black')\n",
"S += sphere((.45, .1,.15),size=.1, color='white') \\\n",
" + sphere((.51, .1,.17), size=.05, color='black')\n",
"S += sphere((.5,0,-.2),size=.1, color='yellow')\n",
"def f(x,y): return math.exp(x/5)*math.cos(y)\n",
"P = plot3d(f,(-5,5),(-5,5), adaptive=True, color=['red','yellow'], max_depth=10)\n",
"cape_man = P.scale(.2) + S.translate(1,0,0)\n",
"cape_man.show(aspect_ratio=[1,1,1], viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"theta, z = var('theta z')\n",
"cylindrical_plot3d(e^(-z^2)*(cos(4*theta)+2)+1, (theta,0,2*pi), (z,-2,2),\n",
" plot_points=[80,80]).show(aspect_ratio=(1,1,1), viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"L = plot3d(lambda x,y: 0, (-5,5), (-5,5), color=\"lightblue\", opacity=0.8)\n",
"P = plot3d(lambda x,y: 4 - x^3 - y^2, (-2,2), (-2,2), color='green')\n",
"Q = plot3d(lambda x,y: x^3 + y^2 - 4, (-2,2), (-2,2), color='orange')\n",
"show(L + P + Q, viewer='threejs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples from [*Parametric plots*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/parametric_plot3d.html)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u = var('u')\n",
"sage: parametric_plot3d((sin(u), cos(u), u/10), (u,0,20), viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"u, v = var('u,v')\n",
"parametric_plot3d((cos(u), sin(u)+cos(v), sin(v)), (u,0,2*pi), (v,-pi,pi), \n",
" mesh=True, viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cm = colormaps.ocean\n",
"def c(x,y): return sin(x*y)**2\n",
"from sage.plot.plot3d.parametric_surface import MoebiusStrip\n",
"MoebiusStrip(5, 1, plot_points=200, color=(c,cm), viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v))\n",
"f2 = (8+(3+cos(v))*cos(u), 3+sin(v), 4+(3+cos(v))*sin(u))\n",
"p1 = parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), texture=\"red\")\n",
"p2 = parametric_plot3d(f2, (u,0,2*pi), (v,0,2*pi), texture=\"blue\")\n",
"(p1 + p2).show(viewer='threejs')"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"K = sqrt(0.84)\n",
"G = (0.4*((K*cosh(0.4*u))^2 + (0.4*sin(K*v))^2))\n",
"f_x = (2*K*cosh(0.4*u)*(-(K*cos(v)*cos(K*v)) - sin(v)*sin(K*v)))/G\n",
"f_y = (2*K*cosh(0.4*u)*(-(K*sin(v)*cos(K*v)) + cos(v)*sin(K*v)))/G\n",
"f_z = -u + (2*0.84*cosh(0.4*u)*sinh(0.4*u))/G\n",
"parametric_plot3d([f_x, f_y, f_z], (u,-13.2,13.2), (v,-37.4,37.4), plot_points=[90,90], \n",
" frame=False, color=\"green\", viewer='threejs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples from [*Surfaces of revolution*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/revolution_plot3d.html)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"curve = (u, cos(4*u), u^2)\n",
"P = revolution_plot3d(curve, (u,0,2), show_curve=True, parallel_axis='z',\n",
" axis=(1,.2), opacity=0.5)\n",
"P.show(aspect_ratio=(1,1,1), viewer='threejs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples from [*Plotting 3D fields*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/plot_field3d.html)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Graphics3d Object"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),\n",
" colors=['red','green','blue'], viewer='threejs')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" ## Examples from [*Implicit Plots*](http://doc.sagemath.org/html/en/reference/plot3d/sage/plot/plot3d/implicit_plot3d.html)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"