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.

Subsection 7.2.1 Size

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) tuple.
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.

Subsection 7.2.2 Edge Labels

Let’s add some edge labels. Within the list of edge tuples, the first two values are vertices, and the third value is the edge label.

Subsection 7.2.3 Color

There are various ways to specify vertex_colors, including hexadecimal, RGB, and color name. Hexadecimal and RGB offer greater flexibility because Sage does not have a name for every color. The color is the dictionary key, and the vertex is the dictionary value.
The following example specifies the color with RGB values. The values can range anywhere from 0 to 1. Color the vertex r red by setting the first element in the RGB tuple to 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. Notice vertex g is darker because the green RGB value is .65 instead of 1.
The following example specifies the color by name instead of RGB value. Sage will return an error if you use an undefined color name.
Let’s specify the edge_colors with RGB values. The edge from vertex r to vertex g is yellow because the RGB tuple sets red and green light to full intensity with no blue light. For darker shades, use values less than 1.
This alternate method specifies the color by name instead:
Consider accessibility when choosing 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.
Here is a Sage Interact to help identify hexadecimal color values.
  • First, click Evaluate (Sage) to define and load the interact. You are welcome to modify the interact definition to suit your needs.
  • You may define a new edge list, vertex size, and graph border within an input box.
  • After entering new values, press Enter on your keyboard to load the new graph.
  • Click on the color selector square to change the color. The hexadecimal value appears to the right of the color square.
  • After selecting a new color, the graph will update when you click outside the color selector.

Subsection 7.2.4 Layout

Let’s define and examine the following graph. Evaluate this cell multiple times and notice the vertex positions are not consistent.
Layout options include: “acyclic”, “circular”, “ranked”, “graphviz”, “planar”, “spring”, or “tree”.
A planar graph can be drawn without any crossing edges. The default graph layout does not ensure the planar layout of a planar graph. Sage will return an error if you try to plot a non-planar graph with the planar layout.
Sage’s planar algorithm sets the vertex positions. Alternatively, we can specify the positions in a dictionary. Let’s position 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 not planar. The circular layout organizes the vertices for improved readability.

Subsection 7.2.5 View in a New Tab

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.

Subsection 7.2.6 Edge Style

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.

Subsection 7.2.7 3-Dimensional

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 touchpad.