From f446f992b701466b9831eee11db77f60349643e6 Mon Sep 17 00:00:00 2001 From: Samuel-Lubliner Date: Tue, 17 Sep 2024 00:24:21 +0000 Subject: [PATCH 1/2] clear previous plot option section --- source/graph-theory/sec-plot-options.ptx | 209 +---------------------- 1 file changed, 2 insertions(+), 207 deletions(-) diff --git a/source/graph-theory/sec-plot-options.ptx b/source/graph-theory/sec-plot-options.ptx index d794928..fb8b379 100644 --- a/source/graph-theory/sec-plot-options.ptx +++ b/source/graph-theory/sec-plot-options.ptx @@ -1,210 +1,5 @@ +
Plot Options - -

- Visualizing graphs in Sage not only provides insights into the structure and properties of the graph but also offers flexibility through customization options. These options include adjusting colors, edge thickness, vertex size, and more, allowing for clearer representation and better understanding of complex graphs. This subsection will guide you through various customization options for graph visualization in Sage. -

- graph plottingedge color -

- To begin with, you can customize the color of vertices and edges. This is particularly useful for highlighting specific parts of a graph, such as a path or a subgraph. -

-

- Here is an example of how to set different colors for vertices and edges: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(vertex_color='blue', edge_color='red') - - - # A graphical representation of the graph with blue vertices and red edges. - - - graph plottingedge thickness -

- Adjusting the thickness of edges can also enhance the visualization, especially for weighted graphs or to emphasize certain edges over others. -

-

- To modify the edge thickness: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(edge_thickness=2.5) # Adjusts the thickness of all edges - - - # A graphical representation of the graph with thicker edges. - - -

- The edge_thickness parameter allows you to specify the thickness of the edges in the plot. -

- graph plottingvertex size -

- Another useful customization is adjusting the size of the vertices, which can be helpful when dealing with graphs that have a large number of vertices or when you want to fit certain texts inside of the vertices. -

-

- To change the size of vertices: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(vertex_size=2000) # Adjusts the size of all vertices - - - # A graphical representation of the graph with larger vertices. - - -

- In order to avoid the previous graph cropping, we can use -

- graph plottingborder - - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(vertex_size=2000, graph_border=True) - - - # A graphical representation of the graph with larger vertices. - - -

- While customizing labels, Sage allows you to enable or disable labels for both vertices and edges, providing clarity and additional context to the graph. However, note that setting specific colors for edge labels directly through the plot method is not supported. -

- graph plottinglabels -

- To customize labels and enable them for vertices and edges: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(vertex_labels=True, edge_labels=True) - - - # A graphical representation of the graph with vertex and edge labels enabled. - - - graph plottinglayouts -

- Layouts in Sage determine how vertices are positioned on the plane. SageMath provides several standard layout algorithms, each offering a unique perspective on the graph's structure. -

-

- For instance, the circular layout places vertices in a circle, which can highlight the symmetry and regular structure of a graph. -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(layout='circular') - - - # Visualizes the graph with vertices positioned in a circular layout. - - -

- The spring layout, also known as a force-directed layout, positions vertices to minimize edge intersections and edge length variance, resembling a system of springs. This layout often reveals the underlying structure of the graph by separating clusters of nodes. -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(layout='spring') - - - # Displays the graph using a spring (force-directed) layout. - - -graph plottingplanar -

- Another common layout is the planar layout, which is applied to graphs that can be drawn without any edge crossings. -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(layout='planar') - - - # Attempts to display the graph with a planar layout, if possible. If the graph is not planar, an error message is printed. - - -

- After exploring the standard layouts, you might want to delve into more sophisticated layout algorithms offered by Graphviz. By setting the layout option to graphviz, you can access various Graphviz layout engines. For instance, prog='dot' is great for hierarchical graphs, prog='neato' for undirected graphs with spring model layouts, prog='fdp' for force-directed layouts, prog='twopi' for radial layouts, and prog='circo' for circular layouts. These layouts are particularly useful when the graph's structure needs to be visualized in a way that standard layouts cannot accommodate. -

-

- Here is an example using a Graphviz layout: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - G.plot(layout='graphviz', prog='dot') - - - # The graph is rendered using the Graphviz 'neato' layout. - - - - -

- Changing the shape of the vertices can be useful for differentiating types of vertices or just for aesthetic purposes. -

-

- To set the vertex shape to a square: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - # above from previous code - G.plot(vertex_shape='s') # Changes the shape of vertices to squares - - - # A graphical representation of the graph with square-shaped vertices. - - - -

- Customizing the style of the edges can help in distinguishing different types of relationships or interactions between vertices. -

-

- To change the edge style to dashed lines: -

- - - G = Graph([('A', 'B'), ('B', 'C'), ('C', 'D'), ('D', 'E'), ('E', 'A')]) - # above from previous code - G.plot(edge_style='dashed') # Changes the edges to dashed lines - - - # A graphical representation of the graph with dashed edges. - - - -

- Below is an example how to apply multiple options to a plot: -

- - - # Creating the graph with its edges - G = Graph( - [ - ('A', 'B'), - ('B', 'C'), - ('C', 'D'), - ('D', 'E'), - ('E', 'A') - ] - ) - G.plot( - layout='circular', - vertex_color='blue', - edge_color='red', - edge_thickness=2.5, - vertex_size=2000, - vertex_labels=True, - edge_labels=True, - vertex_shape='s', - edge_style='dashed' - ) - - - # A graphical representation of the graph incorporating multiple customization options for enhanced visualization. - - +
From 1bb1c24aeea8a952817dae1ea011a2712aa8f013 Mon Sep 17 00:00:00 2001 From: Samuel-Lubliner Date: Wed, 18 Sep 2024 03:16:07 +0000 Subject: [PATCH 2/2] update plot options --- source/graph-theory/sec-plot-options.ptx | 224 ++++++++++++++++++++++- 1 file changed, 223 insertions(+), 1 deletion(-) diff --git a/source/graph-theory/sec-plot-options.ptx b/source/graph-theory/sec-plot-options.ptx index fb8b379..8fe25a4 100644 --- a/source/graph-theory/sec-plot-options.ptx +++ b/source/graph-theory/sec-plot-options.ptx @@ -1,5 +1,227 @@
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: +

+ + + E = [ + ('red', 'green'), + ('green', 'blue'), + ('blue', 'red') + ] + + Graph(E).show() + + + graph plottingvertex size +

+ Increase the vertex_size to fit the vertex label. +

+ + + Graph(E).plot(vertex_size=1000).show() + + + graph plottingfigsize +

+ Resolve the cropping by increasing the figsize. Specify a single number or a [width, height] list. +

+ + + Graph(E).plot(vertex_size=1000, figsize=10).show() + + + graph plottingfigsize +

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

+ + + Graph(E).plot(vertex_size=1000, graph_border=True).show() + + +

+ Let's add some edge labels: +

+ + + E = [ + ('red', 'green', 'yellow'), + ('green', 'blue', 'cyan'), + ('blue', 'red', 'magenta') + ] + + G = Graph(E).plot( + edge_labels=True, + vertex_size=1000, + graph_border=True + ) + + G.show() + + + graph plottingvertex color +

+ There are various ways to set the 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. +

+ + + set_vertex_colors = { + 'red': ['red'], + 'green': ['green'], + 'blue': ['blue'], + } + + G = Graph(E).plot( + vertex_colors=set_vertex_colors, + edge_labels=True, + vertex_size=1000, + graph_border=True + ) + + G.show() + + + graph plottingedge color +

+ Now let's set the edge_colors: +

+ + + set_edge_colors = { + 'yellow': [('red', 'green')], + 'cyan': [('green', 'blue')], + 'magenta': [('blue', 'red')] + } + + G = Graph(E).plot( + edge_colors=set_edge_colors, + vertex_colors=set_vertex_colors, + edge_labels=True, + vertex_size=1000, + graph_border=True + ) + + G.show() + + +

+ 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. If you are trying to contrast between two colors, Blue and Red are usually a safe bet. +

+

+ Let's examine the following graph. Evaluate this cell multiple times and notice the vertex positions are not consistent. +

+ + + N = [ + ('g', 'b',), + ('g', 'd',), + ('g', 'f',), + ('b', 'd',), + ('b', 'f',), + ('d', 'f',), + ] + + G = Graph(N) + + G.show() + + + graph plottinglayout +

+ Setting the layout to planar ensures the lines are not overlapping. If the graph does not support a planar layout, Sage will return an error. +

+ + + G.plot(layout='planar').show() + + +

+ Sage's planar algorithm sets the vertex positions. Alteratively, we can set the positions in a dictionary. Let's set the position of the G node in the center. +

+ + + positions = { + 'g': (0, 0), + 'd': (-1, 1), + 'b': (1, 1), + 'f': (0, -1) + } + + G.plot(pos=positions).show() + + +

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

+ + + I = [ + ("c", "d", "M2"), ("c", "e", "M3"), ("c", "f", "P4"), ("c", "g", "P5"), ("c", "a", "M6"), ("c", "b", "M7"), + ("d", "e", "M2"), ("d", "f", "m3"), ("d", "g", "P4"), ("d", "a", "P5"), ("d", "b", "M6"), ("d", "c", "m7"), + ("e", "f", "m2"), ("e", "g", "m3"), ("e", "a", "P4"), ("e", "b", "P5"), ("e", "c", "m6"), ("e", "d", "m7"), + ("f", "g", "M2"), ("f", "a", "M3"), ("f", "b", "a4"), ("f", "c", "P5"), ("f", "d", "M6"), ("f", "e", "M7"), + ("g", "a", "M2"), ("g", "b", "M3"), ("g", "c", "P4"), ("g", "d", "P5"), ("g", "e", "M6"), ("g", "f", "m7"), + ("a", "b", "M2"), ("a", "c", "m3"), ("a", "d", "P4"), ("a", "e", "P5"), ("a", "f", "m6"), ("a", "g", "m7"), + ("b", "c", "m2"), ("b", "d", "m3"), ("b", "e", "P4"), ("b", "f", "d5"), ("b", "g", "m6"), ("b", "a", "m7"), + ] + + C = DiGraph(I, multiedges=True,) + + C.plot(edge_labels=True) + + +

+ In this case, the graph is incompatible with a planar layout. The circular layout organizes the vertices for improved readability. +

+ + + C.plot(edge_labels=True, layout='circular') + + +

+ Increasing the figsize improves the definition of the arrows. For an even better view of the Graph, right-click the image and view in a new tab. +

+ + + C.plot( + edge_labels=True, + layout='circular', + figsize=30 + ) + + + graph plottingedge style +

+ The options for edge_style include “solid”, “dashed”, “dotted”, or "dashdot”. +

+ + + C.plot( + edge_style='dashed', + edge_labels=True, + layout='circular', + figsize=30 + ) + + +

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

+ + + C.plot( + edge_style='dashed', + color_by_label=True, + edge_labels=True, + layout='circular', + figsize=30 + ) + +