Skip to main content

Discrete Math with SageMath: Learn math with open-source software

Section 7.2 Plot Options

The show() method displays the graphics object immediately with default settings. The plot() method accepts options for customizing the presentation of the graphics object. You can import more features from Matplotlib or for fine-tuned customization options. Let’s examine how the plotting options improve the presentation and help us discover insights into the structure and properties of a graph. The presentation of a Sage graphics object may differ depending on your environment.
Here is a graph that models the primary colors of the RGB color wheel:
Let’s increase the vertex_size:
Resolve the cropping by increasing the figsize. Specify a single number or a [width, height] list.
Increasing the figsize works well in a notebook environment. However, in a SageCell, a large figsize introduces scrolling. Setting graph_border=True is an alternate way to resolve the cropping while maintaining the size of the graph.
Let’s add some edge labels:
There are various ways to set vertex_colors, including hexadecimal, RGB, and color name. Hexadecimal and RGB offer greater flexibility for color names that Sage does not recognize. The color is the dictionary key, and the vertex is the value.
The following example uses (R,G,B) set the color. Color the vertex r red by setting the first element in the (R,G,B) tuple at full intensity with a value of 1. Next ensure vertex r contains no green or blue light by setting the remaining tuple elements to 0.
The following example sets the color by name instead of (R,G,B) value.
Let’s set the edge_colors with (R,G,B) values. The edge from vertex r to vertex g is yellow because the (R,G,B) tuple sets red and green light at full intensity with no blue light.
This alternate method identifies the color by name instead:
Consider accessibility when setting colors on a graph. For example, the red and green on the above graph look indistinguishable to people with color blindness. Blue and Red are usually a safe bet for contrasting two colors.
Let’s examine the following graph. Evaluate this cell multiple times and notice the vertex positions are not consistent.
The planar layout ensures the lines are not overlapping. Sage will return an error if the graph does not support a planar layout.
Sage’s planar algorithm sets the vertex positions. Alternatively, we can set the positions in a dictionary. Let’s set the position of the G node in the center.
The following graph modeling the intervals in the C major scale is challenging to read. Let’s think about how we can improve the presentation.
In this case, the graph is incompatible with a planar layout. The circular layout organizes the vertices for improved readability.
Increasing the figsize improves the definition of the arrows. For an even better view of the Graph, right-click the image and view it in a new tab.
The options for edge_style include “solid”, “dashed”, “dotted”, or "dashdot”.
Improve the definition between the edges by using a different color for each edge. The color_by_label method automatically maps the colors to edges.
View a 3D representation of graph with show3d(). Click and drag the image to change the perspective. Zoom in on the image by pinching your computer’s touch pad.