#!/usr/bin/env python # coding: utf-8 # # 3-dimensional plots with SageMath # In[1]: x, y = var('x y') plot3d(sin(x*y), (x,-pi,pi), (y,-pi,pi), color='green') # In[2]: g1 = plot3d(sin(x*y), (x,-pi,pi), (y,-pi,pi), color='green', plot_points=100) g1 # In[3]: g2 = sphere() g1 + g2 # SageMath's default 3D viewer is `threejs`; it has many options, which are listed [here](https://doc.sagemath.org/html/en/reference/plot3d/threejs.html). # # Another 3D viewer is `tachyon`, which produces static images: # In[4]: show(g1 + g2, viewer='tachyon', aspect_ratio=1, figsize=10) # ## Parametric plots # In[5]: u, v = var('u v') f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v)) f2 = (8+(3+cos(v))*cos(u), 3+sin(v), 4+(3+cos(v))*sin(u)) g1 = parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), color="red") g2 = parametric_plot3d(f2, (u,0,2*pi), (v,0,2*pi), color="gold") g1 + g2 # ## Implicit plots # In[6]: phi = golden_ratio() z = var('z') F = 2 - (cos(x+phi*y) + cos(x-phi*y) + cos(y+phi*z) + cos(y-phi*z) + cos(z-phi*x) + cos(z+phi*x)) r = 4.77 implicit_plot3d(F, (x,-r,r), (y,-r,r), (z,-r,r), plot_points=40, color='darkkhaki') # ## Animated 3D plots # In[7]: n = 20 animate([plot3d(sin((x - k*pi/(n-1))*y), (x, -pi, pi), (y, -pi, pi)) for k in range(n)]).interactive(delay=10)