This is a plotting cheatsheet for SageMath open-source mathematics software system.
- Basic Plot
- Regions
- Vector Fields
- Plotting Geometrical Figures
- Plotting in the Complex Plane
- Plotting Matrices
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)
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)
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')
sage: x, y = var('x, y')
sage: plot_vector_field((y, -x), (x, -2, 2), (y, -2, 2), aspect_ratio=1)
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')
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
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)
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")
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')
<3