Skip to main content

Subsection 1.4 Plotting in Sage

In this subsection we briefly demonstrate how to plot graphs of 2D and 3D objects. Graph plotted in SageMath can be exported in various format such ’pdf’,’png’,’ps’,’eps’ etc.
Listing some of the standard functions in SageMath which can be used for plotting 2D graphs. Readers are advised to go through the plotting section of the help manual and explore more. Some of the standard plotting function are as follows.
Functions Name Explanation
plot Graph of explicitly defined function \(y=f(x)\text{.}\)
parametric_plot Plotting 2D parametric curves.
polar_plot Plotting 2D curves defined in polar coordinates.
implicit_plot Plotting 2D curves defined implicitly.
line Plotting line between two points.
points Plotting scatter points.
circle Plotting circles with given center and radius.
polygon Plotting polygons with given vertices.
complex_plot Plotting level curves of complex functions.
plot_vector_field Plotting vector fields.
matrix_plot Plotting matrix entries.
Reader can get help on each of the above functions and explore how to use them.

Example 1.4.5.

Graph of \(y=x\sin\frac{1}{x}\)
SageMath provides various options to render graphs in desired look and feel for clarity. One can change thickness, color, title, figure size, labeling axes, background, grids etc. Let us look at some of these options.

Example 1.4.6.

Plotting several graphs together.

Example 1.4.7.

Scatter Plots.

Example 1.4.8.

Creating array of graphs.
3D plotting in Sage: Several functions exist to deal with 3D plotting in Sage. We shall just demonstrate a few and leave it to the readers to explore the rest.
Function Name Explanation
plot3d Graph of explicitly defined function \(z=f(x,y)\text{.}\)
parametric_plot3d Plotting parametric surfaces.
implicit_plot3d Plotting 3D surfaces defined implicitly.
arrow3d Plotting 3D arrows.
points3d Plotting scatter points.
line3d Plotting line between two points.
Sphere Plotting 3D sphere.
Torus Plotting 3D torus.
polygon3d Draw a polygon in 3D.
spherical_plot3d Plots a function in spherical coordinates.
cylindrical_plot3d Plots a function in cylindrical coordinates.
Reader can get help on each of the above functions and explore how to use them.

Example 1.4.9.

Plotting graph of the function \(f(x,y)=\sin(x^2+y^2)\text{.}\) We also plot the contours in the same domain.
Let us plot the contours for the above function.

Example 1.4.10.

Let us plot the graph of the surface defined by the following
\begin{equation*} x=\cos(u), y=\sin(u) + \cos(v), z=\sin(v), 0\leq u\leq 2\pi, -\pi\leq v\leq \pi. \end{equation*}

Example 1.4.11. Sage Interact.

SageMath provides a way to create an interactive application for users. This is done using the SageMath function @interact. One can create interactive applications using input boxes, sliders, and selectors. For several examples on Sage @interact, the reader can refer to Sage Interact Wiki.