Lecture 26: Plotting in Three Dimensions and Beyond¶

The default installation of Sage may not come up with the Coffee code (Jmol) needed to manipulate graphics objects. A work around (is needed on Windows computers) is to use tachyon every bit the viewer. In detail, if p is the result of a plotting command, render the plot p as

                        prove            (            p            ,            viewer            =            'tachyon'            )          

Of grade, every bit always, the SageMath Cell Server and CoCalc are as valid alternatives.

In three dimensions, nosotros distinguish betwixt surfaces and space curves. A surface may be given in three unlike ways. Which command to utilise depends on the definition of the surface.

  1. Given every bit a function \(z = f(x, y)\), use plot3d .
  2. Implicitly every bit an equation \(f(x,y,z) = 0\), use implicit_plot3d .
  3. In parameter course as \((x(s,t), y(due south,t), z(s,t))\), employ parametric_plot3d .

Space curves may be given in two different ways. Which command to use depends on the definition of the space curve.

  1. In parameter form as \((x(t), y(t), z(t)\), use parametric_plot3d .
  2. Implicitly, as the intersection of two equations \(f(x,y,z) = 0\) and \(thousand(x,y,z) = 0\), use implicit_plot3d twice.

Observe there are no special commands for space curves, the parametric_plot3d and implicit_plot3d are used differently.

Surface Plots¶

A surface can be defined as \(z = f(x,y)\), where for each point in the plane with coordinates \((x,y)\) the corresponding summit \(z\) is defined by an expression or role in \(10\) and \(y\). In this case we use plot3d . For instance, to plot the surface defined by \(z = \cos(x y)\), for \((x,y) \in [-\pi, +\pi] \times [-\pi, +\pi]\) we practise:

                            x              ,              y              =              var              (              'x,y'              )              plot3d              (              cos              (              x              *              y              ),              (              x              ,              -              pi              ,              pi              ),              (              y              ,              -              pi              ,              pi              ))            

The plot is shown in Fig. 41.

_images/figplot3dcosxy1.png

Fig. 41 The plot of the surface \(z = \cos(x y)\).

Nosotros observe that the plot is rather flat and this is because the values for \(10\) and \(y\) range from \(-3.14\) to \(+3.14\), where as the \(\cos(\cdot)\) takes values between \(-1\) and \(+1\). To make the plot less flat, we change the aspect_ratio and we brand the plot spin, as follows:

                            plot3d              (              cos              (              x              *              y              ),              (              x              ,              -              pi              ,              pi              ),              (              y              ,              -              pi              ,              pi              ),              \              aspect_ratio              =              (              1              ,              1              ,              2              ),              spin              =              1              )            

Nosotros can change the viewing angle with the methods rotateX() and rotateZ() for example.

                            plot3d              (              cos              (              x              *              y              ),              (              x              ,              -              pi              ,              pi              ),              (              y              ,              -              pi              ,              pi              ),              \              aspect_ratio              =              (              1              ,              i              ,              2              ))              .              rotateX              (              pi              /              ii              )              .              rotateZ              (              pi              /              four              )            

The outcome of the rotation of the viewing angles is shown in Fig. 42.

_images/figplot3dcosxy2.png

Fig. 42 The plot of the surface \(z = \cos(x y)\) with adjusted viewing angles.

By default, the value for opacity is one, nosotros can make the plot more transparant by lowering that value. We can besides increment the number of plot points.

                            plot3d              (              cos              (              x              *              y              ),              (              x              ,              -              pi              ,              pi              ),              (              y              ,              -              pi              ,              pi              ),              aspect_ratio              =              (              one              ,              1              ,              2              ),              \              plot_points              =              [              60              ,              sixty              ],              opacity              =              0.75              )              .              rotateX              (              pi              /              10              )              .              rotateY              (              -              pi              /              10              )            

Encounter Fig. 43 for the corresponding picture.

_images/figplot3dcosxy3.png

Fig. 43 The plot of the surface \(z = \cos(x y)\) with adjusted viewing angles, defined number of plot points, and an opacity factor.

Instead of trying to guess the best number of plot points, we can set adaptive to True . Setting color to 'automatic' chooses a rainbow of colors, we can set the number of colors with num_colors .

                            plot3d              (              cos              (              x              *              y              ),              (              ten              ,              -              pi              ,              pi              ),              (              y              ,              -              pi              ,              pi              ),              adaptive              =              True              ,              \              aspect_ratio              =              (              1              ,              1              ,              two              ),              color              =              'automated'              ,              num_colors              =              256              )            

The colorful plot is shown in Fig. 44.

_images/figplot3dcosxy4.png

Fig. 44 The plot of the surface \(z = \cos(x y)\) with an adaptive choice of plot points and with many colors.

Surfaces may be given in parametric form by three expressions in two variables, say \(u\) and \(5\), as \(x = f_x(u,v), y = f_y(u,v)\), and \(z = f_z(u,five)\). Then we use parametric_plot3d . The example beneath comes from the Sage documentation.

                            u              ,              v              =              var              (              'u, 5'              )              fx              =              cos              (              u              )              *              (              4              *              sqrt              (              1              -              v              ^              2              )              *              sin              (              abs              (              u              ))              ^              abs              (              u              ))              fy              =              sin              (              u              )              *              (              4              *              sqrt              (              1              -              v              ^              2              )              *              sin              (              abs              (              u              ))              ^              abs              (              u              ))              fz              =              5              heart              =              parametric_plot3d              ([              fx              ,              fy              ,              fz              ],              (              u              ,              -              pi              ,              pi              ),              (              v              ,              -              1              ,              one              ),              \              color              =              'red'              )              .              rotateX              (              -              pi              /              9              )              .              rotateY              (              -              pi              /              ii              )              .              rotateZ              (              pi              /              4              )              prove              (              heart              ,              frame              =              False              )            

Observe how we can preclude the bounding box from appearing. The surface is shown in Fig. 45.

_images/figparplot3dheart.png

Fig. 45 The plot of a parametric surface.

Space Curves¶

A space bend is defined past a parametric plot of 3 functions in ane variable. For example, the twisted cubic is divers by \(x = t, y = t^2, z = t^3\).

                            t              =              var              (              't'              )              twisted              =              (              t              ,              t              ^              two              ,              t              ^              three              )              parametric_plot3d              (              twisted              ,              (              t              ,              -              i              ,              ane              ))            

Nosotros can brand the curve look red and thicker.

                            pt              =              parametric_plot3d              (              twisted              ,              (              t              ,              -              1              ,              1              ),              color              =              'ruddy'              ,              thickness              =              5              )              testify              (              pt              )            

The consequence of the parametric_plot3d is shown in Fig. 46.

_images/figplot3dtwisted1.png

Fig. 46 A plot of the space curve \((t, t^2, t^3)\), the twisted cubic.

At that place is a bounding box attribute associated to the plot.

We need to know the bounding box if nosotros want to add together to the plot, so we know which ranges for the 3 coordinates to choose. The twisted cubic is the intersection of a parabolic and a cubic cylinder. The parabolic cylinder has its base of operations in the \((x,y)\)-plane and from the parametric representation of the twisted cubic \((x=t, y=t^2)\), we can derive the implicit equation equally \(ten^2 - y = 0\).

                            ten              ,              y              ,              z              =              var              (              'x,y,z'              )              c2              =              implicit_plot3d              (              x              ^              two              -              y              ,(              x              ,              -              ane              ,              one              ),(              y              ,              0              ,              1              ),              (              z              ,              -              1              ,              one              ))              testify              (              pt              +              c2              )            

The result of the implicit_plot3d is shown in Fig. 47.

_images/figplot3dtwisted2.png

Fig. 47 The twisted cubic \((t, t^2, t^3)\) on the parabolic cylinder \(x^2 - y = 0\).

The cubic cylinder has its base in the \((10, z)\)-plane and its equation can be derived from the parameter representation for the twisted cubic, \((10=t, z=t^iii)\) then the equation is \(x^3 - z = 0\).

                            c3              =              implicit_plot3d              (              10              ^              3              -              z              ,(              x              ,              -              1              ,              1              ),(              y              ,              0              ,              i              ),              (              z              ,              -              1              ,              1              ),              color              =              'green'              )              bear witness              (              pt              +              c2              +              c3              )            

We and then run across the ii cylinders with the twisted cubic as their intersection.

Four Dimensional Plots with Colormaps¶

With colormaps we tin can plot in four dimensions. Allow us first explore the utilize of colormaps. We can color surfaces with a colormap. As an example nosotros take the Moebius strip. First we plot it without colour.

                            from              sage.plot.plot3d.parametric_surface              import              MoebiusStrip              ms              =              MoebiusStrip              (              3              ,              ane              ,              plot_points              =              200              )              .              rotateX              (              -              pi              /              8              )              show              (              ms              )            

The figure is shown in Fig. 48.

_images/figmoebius1.png

Fig. 48 The Moebius strip is a ane-sided surface.

Now we apply a colormap.

                            cm              =              colormaps              .              ocean              def              c              (              x              ,              y              ):              return              sin              (              x              *              y              )              **              2              mscm              =              MoebiusStrip              (              3              ,              one              ,              plot_points              =              200              ,              color              =              (              c              ,              cm              ))              .              rotateX              (              -              pi              /              8              )              show              (              mscm              ,              frame              =              False              ,              viewer              =              'tachyon'              )            

The Moebius strip shown in colors is displayed in Fig. 49.

_images/figmoebius2.png

Fig. 49 A colored one sided surface, the Moebius strip.

We tin can employ colormaps to make four dimensional plots. Consider the cubic root of a complex number.

                            u              ,              5              =              var              (              'u,five'              )              w              =              u              +              I              *              v              z              =              west              ^              3              ten              =              real_part              (              z              )              y              =              imag_part              (              z              )            

We consider any complex number w . Considering u and v are real numbers, and so we tin simplify their real and imaginary parts.

                            D              =              {              real_part              (              u              ):              u              ,              imag_part              (              u              ):              0              ,              real_part              (              v              ):              five              ,              imag_part              (              5              ):              0              }              xx              =              x              .              subs              (              D              )              yy              =              y              .              subs              (              D              )              print              twenty              print              yy            

and and then nosotros come across u^3 - three*u*5^2 and 3*u^2*five - v^3 as the expressions for xx and yy . We tin now plot the surface using the expressions for xx and yy every bit functions of u and v , just as parametric_plot3d((20, yy, u), (u, -1, 1), (v, -1, 1)) .

Why does this represent the cubic root? Well, the pinnacle of the surface is u , the real function of the complex number west we started with. The xx and yy are the real and imaginary parts of the z , where z was obtained past taking the number u + I*v to the third power.

Going to polar coordinates produces a nicer plot.

                            r              ,              t              =              var              (              'r,t'              )              rt              =              {              u              :              r              *              cos              (              t              ),              v              :              r              *              sin              (              t              )}              px              =              twenty              .              subs              (              rt              )              py              =              yy              .              subs              (              rt              )            

and at present we make the plot equally

                            parametric_plot3d              ((              px              ,              py              ,              r              *              cos              (              t              )),              (              r              ,              0              ,              1              ),              (              t              ,              0              ,              2              *              pi              ),              adaptive              =              Truthful              )            

The plot is shown in Fig. l.

_images/figcomplexplot1.png

Fig. l A plot of the real part of the cubic root surface.

Now we would similar as color to utilise the imaginary part, 5 = r*sin(t) .

                            cm              =              colormaps              .              autumn              def              c              (              r              ,              t              ):              return              r              *              sin              (              t              )              cr              =              parametric_plot3d              ((              px              ,              py              ,              r              *              cos              (              t              )),              (              r              ,              0              ,              1              ),              (              t              ,              0              ,              two              *              pi              ),              \              adaptive              =              True              ,              color              =              (              c              ,              cm              ))              .              rotateX              (              pi              /              10              )              .              rotateY              (              -              pi              )              .              rotateZ              (              pi              /              2              )              show              (              cr              ,              frame              =              Fake              ,              viewer              =              'tachyon'              )            

and this produces the plot shown in Fig. 51.

_images/figcomplexplot2.png

Fig. 51 The plot of a Riemann surface.

The peak of the surface is the real part and the color of the surface represents the imaginary function of the cubic root. This is a four dimensional plot, also chosen a Riemann surface.

Assignments¶

  1. Consider \(h = 2 \cos(0.4x) \cos(0.4y) + 5 10 y e^{-(x^two + y^2)} + 3 e^{-((x-2)^two + (y-2)^2)}\). Choose advisable ranges for \(ten\) and \(y\) so that the plot of this surface shows three peaks.
  2. The Viviani bend is a space bend divers by \(x = \cos^two(t)\), \(y = \cos(t) \sin(t)\), and \(z = \sin(t)\). Give the SageMath command to plot this space curve.
  3. The Viviani curve is defined as the intersection of the sphere \(10^2 + y^two + z^2 = 1\) and the cylinder \(10^2 + y^two = i\). Plot the 2 surfaces and emphasize their intersection calculation the result of the plot of the previous practice.
  4. A torus knot is divers by \(r = 2 + 4/5 \cos(7t)\), \(x = r \cos(four t)\), \(y = r \sin(iv t)\), and \(z = \sin(7t)\). Plot this knot with SageMath.
  5. Consider the surface defined past the equation \(f(x,y,z) = x^3 - y^2 - z^ii = 0\).
    1. Give the SageMath control to plot this surface, for \(x \in [0, one]\) and for \(y, z \in [-i, 1]\). What do yous see at (0,0,0)? Describe.
    2. Replace \(z\) by naught and transform the curve divers by \(f(x, y, 0) = 0\) into polar coordinates. Write the SageMath command to graph the curve in polar coordinates. Use good bounds for the range of \(t\).