Skip to content

Instantly share code, notes, and snippets.

@mforets
Last active February 23, 2024 10:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mforets/781d271f7d434cc7af5a99690ff107f5 to your computer and use it in GitHub Desktop.
Save mforets/781d271f7d434cc7af5a99690ff107f5 to your computer and use it in GitHub Desktop.
plotting cheatsheet for SageMath

This is a plotting cheatsheet for SageMath open-source mathematics software system.

Contents

Basic Plot

Function Description
plot 2d plot of a function
sage: f = 2*sin(x)+x; g = 2*sin(x)+x/2
sage: plot(f, x, 0, 3*pi, color='red', fill=g, fillcolor='blue', thickness=2, fillalpha=0.4)

plot_2d_fill

Regions

Function Description
region_plot Plot the region where boolean a function of two variables is true
sage: region_plot(lambda x, y: x^2 - y^2 <= 0, (-1, 1), (-1, 1), \
....:             incol='gold', bordercol='black', borderstyle='dashed', \
....:             aspect_ratio=1, alpha=0.3)

region_plot_cone

Vector Fields

Function Description
streamline_plot Streamline plot in a vector field
plot_vector_field Vector arrows of the functions over the specified ranges
plot_vector_field3d Three-dimensional vector field
sage: x, y = var('x, y')
sage: streamline_plot((y, -x**2 * y - x + y), (x, -3, 3), (y, -3, 3), density=1.4, color='black')

streamline_plot_vanderpol

sage: x, y = var('x, y')
sage: plot_vector_field((y, -x), (x, -2, 2), (y, -2, 2), aspect_ratio=1)

vector_field_circular

Plotting Geometrical Figures

Function Description
Polyhedron.plot() Graphical representation of a polyhedron
circle Plot a circle given its center and radius
point Plot a point given its coordinates
tetrahedron A 3d tetrahedron
sage: polytopes.regular_polygon(8).plot(alpha=0.4) \ 
....: + circle((0, 0), 1, color='black').plot() \ 
....: + point((1/2, 1/2), marker='x', size=40, color='red')

polyhedron_rectangles_plot

This example of tetrahedron is from by John Palmieri in this answer:

def Tetrahedron(vertices, color='red'):
    faces = [(0,1,2), (0,1,3), (0,2,3), (1,2,3)]
    return polygons3d(faces, vertices, color=color)

T1 = Tetrahedron([(0,0,0), (1,0,0), (0,1,0), (0,0,1)], color='red')
T2 = Tetrahedron([(1,0,0), (0,1,0), (0,0,1), (1,0,1)], color='green')
T3 = Tetrahedron([(0,1,0), (0,0,1), (1,0,1), (0,1,1)], color='blue')
T1+T2+T3

tetrahedron

Plotting in the Complex Plane

Function Description
point Plot a point given its coordinates

This example, borrowed from Calcul mathématique avec Sage p.252, plots the distribution of roots of all polynomials with coefficients which are either -1 or 1, up to degree 14:

sage: points(build_complex_roots(14), pointsize=1, aspect_ratio=1)

points_complex_roots

Plotting Matrices

Function Description
matrix_plot Plot a given matrix or 2D array, colored w.r.t. the value of its entries.

To see the available colormaps type import matplotlib.cm; matplotlib.cm.datad.keys().

Plotting a Toeplitz matrix:

sage: Tn = lambda n : matrix.toeplitz([-2, 1] + [0]*(n-2), [1] + [0]*(n-2))
sage: matrix_plot(Tn(100)^50, cmap="coolwarm_r")

matrix_plot

Another matrix which is constant accross diagonals, modulated by an oscillating function (the idea is from Ch. 91, Sage, in Handbook of Linear Algebra, 2nd Ed.):

sage: r = [cos(2*pi*i/25) for i in range(50)]
sage: A = matrix.toeplitz(r, r[1:])
sage: A.plot(colorbar=True, cmap='hsv')

toeplitz_cos

@nikos-kekatos
Copy link

<3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment