Graph plotting¶
(For LaTeX drawings of graphs, see the graph_latex
module.)
All graphs have an associated Sage graphics object, which you can display:
sage: G = graphs.WheelGraph(15)
sage: P = G.plot()
sage: P.show() # long time
>>> from sage.all import *
>>> G = graphs.WheelGraph(Integer(15))
>>> P = G.plot()
>>> P.show() # long time
When plotting a graph created using Sage’s Graph
command,
node positions are determined using the spring-layout algorithm.
Special graphs available from graphs.*
have preset positions.
For example, compare the two plots of the Petersen graph,
as obtained using Graph
or as obtained from that database:
sage: petersen_spring = Graph(':I`ES@obGkqegW~')
sage: petersen_spring.show() # long time
>>> from sage.all import *
>>> petersen_spring = Graph(':I`ES@obGkqegW~')
>>> petersen_spring.show() # long time
sage: petersen_database = graphs.PetersenGraph()
sage: petersen_database.show() # long time
>>> from sage.all import *
>>> petersen_database = graphs.PetersenGraph()
>>> petersen_database.show() # long time
All constructors in this database (except some random graphs) prefill the position dictionary, bypassing the spring-layout positioning algorithm.
Plot options
Here is the list of options accepted by
plot()
and the constructor of
GraphPlot
. Those two functions also accept all options of
sage.plot.graphics.Graphics.show()
.
|
A layout algorithm – one of : “acyclic”, “circular” (plots the graph with vertices evenly distributed on a circle), “ranked”, “graphviz”, “planar”, “spring” (traditional spring layout, using the graph’s current positions as initial positions), or “tree” (the tree will be plotted in levels, depending on minimum distance for the root). |
|
The number of times to execute the spring layout algorithm. |
|
A dictionary mapping heights to the list of vertices at this height. |
|
Use spring layout to finalize the current layout. |
|
A vertex designation for drawing trees. A vertex of the tree to be used as the root for the |
|
An iterable specifying which vertices to use as roots for the |
|
The direction of tree branches – ‘up’, ‘down’, ‘left’ or ‘right’. |
|
Whether or not to save the computed position for the graph. |
|
The dimension of the layout – 2 or 3. |
|
Which graphviz layout program to use – one of “circo”, “dot”, “fdp”, “neato”, or “twopi”. |
|
Whether to do the spring layout by connected component – boolean. |
|
The position dictionary of vertices. |
|
Vertex labels to draw. This can be |
|
Default color for vertices not listed in vertex_colors dictionary. |
|
A dictionary specifying vertex colors: each key is a color recognizable by matplotlib, and each corresponding value is a list of vertices. |
|
The size to draw the vertices. |
|
The shape to draw the vertices. Currently unavailable for Multi-edged DiGraphs. |
|
Whether or not to draw edge labels. |
|
The linestyle of the edges. It should be one of “solid”, “dashed”, “dotted”, dashdot”, or “-”, “–”, “:”, “-.”, respectively. |
|
The thickness of the edges. |
|
The default color for edges not listed in edge_colors. |
|
A dictionary specifying edge colors: each key is a color recognized by matplotlib, and each corresponding value is a list of edges. |
|
Whether to color the edges according to their labels. This also accepts a function or dictionary mapping labels to colors. |
|
A partition of the vertex set. If specified, plot will show each cell in a different color; vertex_colors takes precedence. |
|
The radius of the smallest loop. |
|
The distance between multiedges. |
|
The max distance range to allow multiedges. |
|
Whether to display the vertices in talk mode (larger and white). |
|
Whether or not to draw a frame around the graph. |
|
The color of the background of the edge labels. |
Default options
This module defines two dictionaries containing default options for the
plot()
and
show()
methods. These two
dictionaries are sage.graphs.graph_plot.DEFAULT_PLOT_OPTIONS
and
sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS
, respectively.
Obviously, these values are overruled when arguments are given explicitly.
Here is how to define the default size of a graph drawing to be (6, 6)
.
The first two calls to show()
use this option, while the third does not (a value for figsize
is explicitly given):
sage: import sage.graphs.graph_plot
sage: sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (6, 6)
sage: graphs.PetersenGraph().show() # long time
sage: graphs.ChvatalGraph().show() # long time
sage: graphs.PetersenGraph().show(figsize=(4, 4)) # long time
>>> from sage.all import *
>>> import sage.graphs.graph_plot
>>> sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (Integer(6), Integer(6))
>>> graphs.PetersenGraph().show() # long time
>>> graphs.ChvatalGraph().show() # long time
>>> graphs.PetersenGraph().show(figsize=(Integer(4), Integer(4))) # long time
We can now reset the default to its initial value, and now display graphs as previously:
sage: sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (4, 4)
sage: graphs.PetersenGraph().show() # long time
sage: graphs.ChvatalGraph().show() # long time
>>> from sage.all import *
>>> sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (Integer(4), Integer(4))
>>> graphs.PetersenGraph().show() # long time
>>> graphs.ChvatalGraph().show() # long time
Note
While
DEFAULT_PLOT_OPTIONS
affects bothG.show()
andG.plot()
, settings fromDEFAULT_SHOW_OPTIONS
only affectsG.show()
.In order to define a default value permanently, you can add a couple of lines to Sage’s startup scripts. Example:
sage: import sage.graphs.graph_plot sage: sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (4, 4)
>>> from sage.all import * >>> import sage.graphs.graph_plot >>> sage.graphs.graph_plot.DEFAULT_SHOW_OPTIONS['figsize'] = (Integer(4), Integer(4))
Index of methods and functions
Set the position plotting parameters for this GraphPlot. |
|
Set the vertex plotting parameters for this GraphPlot. |
|
Set the edge (or arrow) plotting parameters for the GraphPlot object. |
|
Show the (Di)Graph associated with this GraphPlot object. |
|
Return a graphics object representing the (di)graph. |
|
Compute a nice layout of a tree. |
- class sage.graphs.graph_plot.GraphPlot(graph, options)[source]¶
Bases:
SageObject
Return a
GraphPlot
object, which stores all the parameters needed for plotting (Di)Graphs.A
GraphPlot
has a plot and show function, as well as some functions to set parameters for vertices and edges. This constructor assumes default options are set. Defaults are shown in the example below.EXAMPLES:
sage: from sage.graphs.graph_plot import GraphPlot sage: options = { ....: 'vertex_size': 200, ....: 'vertex_labels': True, ....: 'layout': None, ....: 'edge_style': 'solid', ....: 'edge_color': 'black', ....: 'edge_colors': None, ....: 'edge_labels': False, ....: 'iterations': 50, ....: 'tree_orientation': 'down', ....: 'heights': None, ....: 'graph_border': False, ....: 'talk': False, ....: 'color_by_label': False, ....: 'partition': None, ....: 'dist': .075, ....: 'max_dist': 1.5, ....: 'loop_size': .075, ....: 'edge_labels_background': 'transparent'} sage: g = Graph({0: [1, 2], 2: [3], 4: [0, 1]}) sage: GP = GraphPlot(g, options)
>>> from sage.all import * >>> from sage.graphs.graph_plot import GraphPlot >>> options = { ... 'vertex_size': Integer(200), ... 'vertex_labels': True, ... 'layout': None, ... 'edge_style': 'solid', ... 'edge_color': 'black', ... 'edge_colors': None, ... 'edge_labels': False, ... 'iterations': Integer(50), ... 'tree_orientation': 'down', ... 'heights': None, ... 'graph_border': False, ... 'talk': False, ... 'color_by_label': False, ... 'partition': None, ... 'dist': RealNumber('.075'), ... 'max_dist': RealNumber('1.5'), ... 'loop_size': RealNumber('.075'), ... 'edge_labels_background': 'transparent'} >>> g = Graph({Integer(0): [Integer(1), Integer(2)], Integer(2): [Integer(3)], Integer(4): [Integer(0), Integer(1)]}) >>> GP = GraphPlot(g, options)
- layout_tree(root, orientation)[source]¶
Compute a nice layout of a tree.
INPUT:
root
– the root vertexorientation
– whether to place the root at the top or at the bottom:orientation="down"
– children are placed below their parentorientation="top"
– children are placed above their parent
EXAMPLES:
sage: from sage.graphs.graph_plot import GraphPlot sage: G = graphs.HoffmanSingletonGraph() sage: T = Graph() sage: T.add_edges(G.min_spanning_tree(starting_vertex=0)) sage: T.show(layout='tree', tree_root=0) # indirect doctest
>>> from sage.all import * >>> from sage.graphs.graph_plot import GraphPlot >>> G = graphs.HoffmanSingletonGraph() >>> T = Graph() >>> T.add_edges(G.min_spanning_tree(starting_vertex=Integer(0))) >>> T.show(layout='tree', tree_root=Integer(0)) # indirect doctest
- plot(**kwds)[source]¶
Return a graphics object representing the (di)graph.
INPUT:
The options accepted by this method are to be found in the documentation of the
sage.graphs.graph_plot
module, and theshow()
method.Note
See
the module's documentation
for information on default values of this method.We can specify some pretty precise plotting of familiar graphs:
sage: from math import sin, cos, pi sage: P = graphs.PetersenGraph() sage: d = {'#FF0000': [0, 5], '#FF9900': [1, 6], '#FFFF00': [2, 7], ....: '#00FF00': [3, 8], '#0000FF': [4,9]} sage: pos_dict = {} sage: for i in range(5): ....: x = float(cos(pi/2 + ((2*pi)/5)*i)) ....: y = float(sin(pi/2 + ((2*pi)/5)*i)) ....: pos_dict[i] = [x,y] ... sage: for i in range(5, 10): ....: x = float(0.5*cos(pi/2 + ((2*pi)/5)*i)) ....: y = float(0.5*sin(pi/2 + ((2*pi)/5)*i)) ....: pos_dict[i] = [x,y] ... sage: pl = P.graphplot(pos=pos_dict, vertex_colors=d) sage: pl.show()
>>> from sage.all import * >>> from math import sin, cos, pi >>> P = graphs.PetersenGraph() >>> d = {'#FF0000': [Integer(0), Integer(5)], '#FF9900': [Integer(1), Integer(6)], '#FFFF00': [Integer(2), Integer(7)], ... '#00FF00': [Integer(3), Integer(8)], '#0000FF': [Integer(4),Integer(9)]} >>> pos_dict = {} >>> for i in range(Integer(5)): ... x = float(cos(pi/Integer(2) + ((Integer(2)*pi)/Integer(5))*i)) ... y = float(sin(pi/Integer(2) + ((Integer(2)*pi)/Integer(5))*i)) ... pos_dict[i] = [x,y] ... >>> for i in range(Integer(5), Integer(10)): ... x = float(RealNumber('0.5')*cos(pi/Integer(2) + ((Integer(2)*pi)/Integer(5))*i)) ... y = float(RealNumber('0.5')*sin(pi/Integer(2) + ((Integer(2)*pi)/Integer(5))*i)) ... pos_dict[i] = [x,y] ... >>> pl = P.graphplot(pos=pos_dict, vertex_colors=d) >>> pl.show()
Here are some more common graphs with typical options:
sage: C = graphs.CubeGraph(8) sage: P = C.graphplot(vertex_labels=False, vertex_size=0, ....: graph_border=True) sage: P.show()
>>> from sage.all import * >>> C = graphs.CubeGraph(Integer(8)) >>> P = C.graphplot(vertex_labels=False, vertex_size=Integer(0), ... graph_border=True) >>> P.show()
sage: G = graphs.HeawoodGraph().copy(sparse=True) sage: for u, v, l in G.edges(sort=True): ....: G.set_edge_label(u, v, f'({u},{v})') sage: G.graphplot(edge_labels=True).show()
>>> from sage.all import * >>> G = graphs.HeawoodGraph().copy(sparse=True) >>> for u, v, l in G.edges(sort=True): ... G.set_edge_label(u, v, f'({u},{v})') >>> G.graphplot(edge_labels=True).show()
The options for plotting also work with directed graphs:
sage: D = DiGraph({ ....: 0: [1, 10, 19], 1: [8, 2], 2: [3, 6], 3: [19, 4], ....: 4: [17, 5], 5: [6, 15], 6: [7], 7: [8, 14], 8: [9], ....: 9: [10, 13], 10: [11], 11: [12, 18], 12: [16, 13], ....: 13: [14], 14: [15], 15: [16], 16: [17], 17: [18], ....: 18: [19], 19: []}) sage: for u, v, l in D.edges(sort=True): ....: D.set_edge_label(u, v, f'({u},{v})') sage: D.graphplot(edge_labels=True, layout='circular').show()
>>> from sage.all import * >>> D = DiGraph({ ... Integer(0): [Integer(1), Integer(10), Integer(19)], Integer(1): [Integer(8), Integer(2)], Integer(2): [Integer(3), Integer(6)], Integer(3): [Integer(19), Integer(4)], ... Integer(4): [Integer(17), Integer(5)], Integer(5): [Integer(6), Integer(15)], Integer(6): [Integer(7)], Integer(7): [Integer(8), Integer(14)], Integer(8): [Integer(9)], ... Integer(9): [Integer(10), Integer(13)], Integer(10): [Integer(11)], Integer(11): [Integer(12), Integer(18)], Integer(12): [Integer(16), Integer(13)], ... Integer(13): [Integer(14)], Integer(14): [Integer(15)], Integer(15): [Integer(16)], Integer(16): [Integer(17)], Integer(17): [Integer(18)], ... Integer(18): [Integer(19)], Integer(19): []}) >>> for u, v, l in D.edges(sort=True): ... D.set_edge_label(u, v, f'({u},{v})') >>> D.graphplot(edge_labels=True, layout='circular').show()
This example shows off the coloring of edges:
sage: from sage.plot.colors import rainbow sage: C = graphs.CubeGraph(5) sage: R = rainbow(5) sage: edge_colors = {} sage: for i in range(5): ....: edge_colors[R[i]] = [] sage: for u, v, l in C.edges(sort=True): ....: for i in range(5): ....: if u[i] != v[i]: ....: edge_colors[R[i]].append((u, v, l)) sage: C.graphplot(vertex_labels=False, vertex_size=0, ....: edge_colors=edge_colors).show()
>>> from sage.all import * >>> from sage.plot.colors import rainbow >>> C = graphs.CubeGraph(Integer(5)) >>> R = rainbow(Integer(5)) >>> edge_colors = {} >>> for i in range(Integer(5)): ... edge_colors[R[i]] = [] >>> for u, v, l in C.edges(sort=True): ... for i in range(Integer(5)): ... if u[i] != v[i]: ... edge_colors[R[i]].append((u, v, l)) >>> C.graphplot(vertex_labels=False, vertex_size=Integer(0), ... edge_colors=edge_colors).show()
With the
partition
option, we can separate out same-color groups of vertices:sage: D = graphs.DodecahedralGraph() sage: Pi = [[6, 5, 15, 14, 7], [16, 13, 8, 2, 4], ....: [12, 17, 9, 3, 1], [0, 19, 18, 10, 11]] sage: D.show(partition=Pi)
>>> from sage.all import * >>> D = graphs.DodecahedralGraph() >>> Pi = [[Integer(6), Integer(5), Integer(15), Integer(14), Integer(7)], [Integer(16), Integer(13), Integer(8), Integer(2), Integer(4)], ... [Integer(12), Integer(17), Integer(9), Integer(3), Integer(1)], [Integer(0), Integer(19), Integer(18), Integer(10), Integer(11)]] >>> D.show(partition=Pi)
Loops are also plotted correctly:
sage: G = graphs.PetersenGraph() sage: G.allow_loops(True) sage: G.add_edge(0,0) sage: G.show()
>>> from sage.all import * >>> G = graphs.PetersenGraph() >>> G.allow_loops(True) >>> G.add_edge(Integer(0),Integer(0)) >>> G.show()
sage: D = DiGraph({0:[0,1], 1:[2], 2:[3]}, loops=True) sage: D.show() sage: D.show(edge_colors={(0, 1, 0): [(0, 1, None), (1, 2, None)], ....: (0, 0, 0): [(2, 3, None)]})
>>> from sage.all import * >>> D = DiGraph({Integer(0):[Integer(0),Integer(1)], Integer(1):[Integer(2)], Integer(2):[Integer(3)]}, loops=True) >>> D.show() >>> D.show(edge_colors={(Integer(0), Integer(1), Integer(0)): [(Integer(0), Integer(1), None), (Integer(1), Integer(2), None)], ... (Integer(0), Integer(0), Integer(0)): [(Integer(2), Integer(3), None)]})
More options:
sage: pos = {0: [0.0, 1.5], 1: [-0.8, 0.3], 2: [-0.6, -0.8], ....: 3:[0.6, -0.8], 4:[0.8, 0.3]} sage: g = Graph({0: [1], 1: [2], 2: [3], 3: [4], 4: [0]}) sage: g.graphplot(pos=pos, layout='spring', iterations=0).plot() Graphics object consisting of 11 graphics primitives
>>> from sage.all import * >>> pos = {Integer(0): [RealNumber('0.0'), RealNumber('1.5')], Integer(1): [-RealNumber('0.8'), RealNumber('0.3')], Integer(2): [-RealNumber('0.6'), -RealNumber('0.8')], ... Integer(3):[RealNumber('0.6'), -RealNumber('0.8')], Integer(4):[RealNumber('0.8'), RealNumber('0.3')]} >>> g = Graph({Integer(0): [Integer(1)], Integer(1): [Integer(2)], Integer(2): [Integer(3)], Integer(3): [Integer(4)], Integer(4): [Integer(0)]}) >>> g.graphplot(pos=pos, layout='spring', iterations=Integer(0)).plot() Graphics object consisting of 11 graphics primitives
sage: G = Graph() sage: P = G.graphplot().plot() sage: P.axes() False sage: G = DiGraph() sage: P = G.graphplot().plot() sage: P.axes() False
>>> from sage.all import * >>> G = Graph() >>> P = G.graphplot().plot() >>> P.axes() False >>> G = DiGraph() >>> P = G.graphplot().plot() >>> P.axes() False
We can plot multiple graphs:
sage: T = list(graphs.trees(7)) sage: t = T[3] sage: t.graphplot(heights={0: [0], 1: [4, 5, 1], ....: 2: [2], 3: [3, 6]} ....: ).plot() Graphics object consisting of 14 graphics primitives
>>> from sage.all import * >>> T = list(graphs.trees(Integer(7))) >>> t = T[Integer(3)] >>> t.graphplot(heights={Integer(0): [Integer(0)], Integer(1): [Integer(4), Integer(5), Integer(1)], ... Integer(2): [Integer(2)], Integer(3): [Integer(3), Integer(6)]} ... ).plot() Graphics object consisting of 14 graphics primitives
sage: T = list(graphs.trees(7)) sage: t = T[3] sage: t.graphplot(heights={0: [0], 1: [4, 5, 1], ....: 2: [2], 3: [3, 6]} ....: ).plot() Graphics object consisting of 14 graphics primitives
>>> from sage.all import * >>> T = list(graphs.trees(Integer(7))) >>> t = T[Integer(3)] >>> t.graphplot(heights={Integer(0): [Integer(0)], Integer(1): [Integer(4), Integer(5), Integer(1)], ... Integer(2): [Integer(2)], Integer(3): [Integer(3), Integer(6)]} ... ).plot() Graphics object consisting of 14 graphics primitives
sage: t.set_edge_label(0, 1, -7) sage: t.set_edge_label(0, 5, 3) sage: t.set_edge_label(0, 5, 99) sage: t.set_edge_label(1, 2, 1000) sage: t.set_edge_label(3, 2, 'spam') sage: t.set_edge_label(2, 6, 3/2) sage: t.set_edge_label(0, 4, 66) sage: t.graphplot(heights={0: [0], 1: [4, 5, 1], ....: 2: [2], 3: [3, 6]}, ....: edge_labels=True ....: ).plot() Graphics object consisting of 20 graphics primitives
>>> from sage.all import * >>> t.set_edge_label(Integer(0), Integer(1), -Integer(7)) >>> t.set_edge_label(Integer(0), Integer(5), Integer(3)) >>> t.set_edge_label(Integer(0), Integer(5), Integer(99)) >>> t.set_edge_label(Integer(1), Integer(2), Integer(1000)) >>> t.set_edge_label(Integer(3), Integer(2), 'spam') >>> t.set_edge_label(Integer(2), Integer(6), Integer(3)/Integer(2)) >>> t.set_edge_label(Integer(0), Integer(4), Integer(66)) >>> t.graphplot(heights={Integer(0): [Integer(0)], Integer(1): [Integer(4), Integer(5), Integer(1)], ... Integer(2): [Integer(2)], Integer(3): [Integer(3), Integer(6)]}, ... edge_labels=True ... ).plot() Graphics object consisting of 20 graphics primitives
sage: T = list(graphs.trees(7)) sage: t = T[3] sage: t.graphplot(layout='tree').show()
>>> from sage.all import * >>> T = list(graphs.trees(Integer(7))) >>> t = T[Integer(3)] >>> t.graphplot(layout='tree').show()
The tree layout is also useful:
sage: t = DiGraph('JCC???@A??GO??CO??GO??') sage: t.graphplot(layout='tree', tree_root=0, ....: tree_orientation="up" ....: ).show()
>>> from sage.all import * >>> t = DiGraph('JCC???@A??GO??CO??GO??') >>> t.graphplot(layout='tree', tree_root=Integer(0), ... tree_orientation="up" ... ).show()
More examples:
sage: D = DiGraph({0:[1,2,3], 2:[1,4], 3:[0]}) sage: D.graphplot().show()
>>> from sage.all import * >>> D = DiGraph({Integer(0):[Integer(1),Integer(2),Integer(3)], Integer(2):[Integer(1),Integer(4)], Integer(3):[Integer(0)]}) >>> D.graphplot().show()
sage: D = DiGraph(multiedges=True, sparse=True) sage: for i in range(5): ....: D.add_edge((i, i + 1, 'a')) ....: D.add_edge((i, i - 1, 'b')) sage: D.graphplot(edge_labels=True, ....: edge_colors=D._color_by_label() ....: ).plot() Graphics object consisting of 34 graphics primitives
>>> from sage.all import * >>> D = DiGraph(multiedges=True, sparse=True) >>> for i in range(Integer(5)): ... D.add_edge((i, i + Integer(1), 'a')) ... D.add_edge((i, i - Integer(1), 'b')) >>> D.graphplot(edge_labels=True, ... edge_colors=D._color_by_label() ... ).plot() Graphics object consisting of 34 graphics primitives
sage: g = Graph({}, loops=True, multiedges=True, sparse=True) sage: g.add_edges([(0, 0, 'a'), (0, 0, 'b'), (0, 1, 'c'), ....: (0, 1, 'd'), (0, 1, 'e'), (0, 1, 'f'), ....: (0, 1, 'f'), (2, 1, 'g'), (2, 2, 'h')]) sage: g.graphplot(edge_labels=True, ....: color_by_label=True, ....: edge_style='dashed' ....: ).plot() Graphics object consisting of 22 graphics primitives
>>> from sage.all import * >>> g = Graph({}, loops=True, multiedges=True, sparse=True) >>> g.add_edges([(Integer(0), Integer(0), 'a'), (Integer(0), Integer(0), 'b'), (Integer(0), Integer(1), 'c'), ... (Integer(0), Integer(1), 'd'), (Integer(0), Integer(1), 'e'), (Integer(0), Integer(1), 'f'), ... (Integer(0), Integer(1), 'f'), (Integer(2), Integer(1), 'g'), (Integer(2), Integer(2), 'h')]) >>> g.graphplot(edge_labels=True, ... color_by_label=True, ... edge_style='dashed' ... ).plot() Graphics object consisting of 22 graphics primitives
The
edge_style
option may be provided in the short format too:sage: g.graphplot(edge_labels=True, ....: color_by_label=True, ....: edge_style='--' ....: ).plot() Graphics object consisting of 22 graphics primitives
>>> from sage.all import * >>> g.graphplot(edge_labels=True, ... color_by_label=True, ... edge_style='--' ... ).plot() Graphics object consisting of 22 graphics primitives
- set_edges(**edge_options)[source]¶
Set edge plotting parameters for the
GraphPlot
object.This function is called by the constructor but can also be called to update the edge options of an existing
GraphPlot
object. Note that the changes are cumulative.EXAMPLES:
sage: g = Graph(loops=True, multiedges=True, sparse=True) sage: g.add_edges([(0, 0, 'a'), (0, 0, 'b'), (0, 1, 'c'), ....: (0, 1, 'd'), (0, 1, 'e'), (0, 1, 'f'), ....: (0, 1, 'f'), (2, 1, 'g'), (2, 2, 'h')]) sage: GP = g.graphplot(vertex_size=100, edge_labels=True, ....: color_by_label=True, edge_style='dashed') sage: GP.set_edges(edge_style='solid') sage: GP.plot() Graphics object consisting of 22 graphics primitives
>>> from sage.all import * >>> g = Graph(loops=True, multiedges=True, sparse=True) >>> g.add_edges([(Integer(0), Integer(0), 'a'), (Integer(0), Integer(0), 'b'), (Integer(0), Integer(1), 'c'), ... (Integer(0), Integer(1), 'd'), (Integer(0), Integer(1), 'e'), (Integer(0), Integer(1), 'f'), ... (Integer(0), Integer(1), 'f'), (Integer(2), Integer(1), 'g'), (Integer(2), Integer(2), 'h')]) >>> GP = g.graphplot(vertex_size=Integer(100), edge_labels=True, ... color_by_label=True, edge_style='dashed') >>> GP.set_edges(edge_style='solid') >>> GP.plot() Graphics object consisting of 22 graphics primitives
sage: GP.set_edges(edge_color='black') sage: GP.plot() Graphics object consisting of 22 graphics primitives
>>> from sage.all import * >>> GP.set_edges(edge_color='black') >>> GP.plot() Graphics object consisting of 22 graphics primitives
sage: d = DiGraph(loops=True, multiedges=True, sparse=True) sage: d.add_edges([(0, 0, 'a'), (0, 0, 'b'), (0, 1, 'c'), ....: (0, 1, 'd'), (0, 1, 'e'), (0, 1, 'f'), ....: (0, 1, 'f'), (2, 1, 'g'), (2, 2, 'h')]) sage: GP = d.graphplot(vertex_size=100, edge_labels=True, ....: color_by_label=True, edge_style='dashed') sage: GP.set_edges(edge_style='solid') sage: GP.plot() Graphics object consisting of 24 graphics primitives
>>> from sage.all import * >>> d = DiGraph(loops=True, multiedges=True, sparse=True) >>> d.add_edges([(Integer(0), Integer(0), 'a'), (Integer(0), Integer(0), 'b'), (Integer(0), Integer(1), 'c'), ... (Integer(0), Integer(1), 'd'), (Integer(0), Integer(1), 'e'), (Integer(0), Integer(1), 'f'), ... (Integer(0), Integer(1), 'f'), (Integer(2), Integer(1), 'g'), (Integer(2), Integer(2), 'h')]) >>> GP = d.graphplot(vertex_size=Integer(100), edge_labels=True, ... color_by_label=True, edge_style='dashed') >>> GP.set_edges(edge_style='solid') >>> GP.plot() Graphics object consisting of 24 graphics primitives
sage: GP.set_edges(edge_color='black') sage: GP.plot() Graphics object consisting of 24 graphics primitives
>>> from sage.all import * >>> GP.set_edges(edge_color='black') >>> GP.plot() Graphics object consisting of 24 graphics primitives
- set_pos()[source]¶
Set the position plotting parameters for this GraphPlot.
EXAMPLES:
This function is called implicitly by the code below:
sage: g = Graph({0: [1, 2], 2: [3], 4: [0, 1]}) sage: g.graphplot(save_pos=True, layout='circular') # indirect doctest GraphPlot object for Graph on 5 vertices
>>> from sage.all import * >>> g = Graph({Integer(0): [Integer(1), Integer(2)], Integer(2): [Integer(3)], Integer(4): [Integer(0), Integer(1)]}) >>> g.graphplot(save_pos=True, layout='circular') # indirect doctest GraphPlot object for Graph on 5 vertices
The following illustrates the format of a position dictionary, but due to numerical noise we do not check the values themselves:
sage: g.get_pos() {0: (0.0, 1.0), 1: (-0.951..., 0.309...), 2: (-0.587..., -0.809...), 3: (0.587..., -0.809...), 4: (0.951..., 0.309...)}
>>> from sage.all import * >>> g.get_pos() {0: (0.0, 1.0), 1: (-0.951..., 0.309...), 2: (-0.587..., -0.809...), 3: (0.587..., -0.809...), 4: (0.951..., 0.309...)}
sage: T = list(graphs.trees(7)) sage: t = T[3] sage: t.plot(heights={0: [0], 1: [4, 5, 1], 2: [2], 3: [3, 6]}) Graphics object consisting of 14 graphics primitives
>>> from sage.all import * >>> T = list(graphs.trees(Integer(7))) >>> t = T[Integer(3)] >>> t.plot(heights={Integer(0): [Integer(0)], Integer(1): [Integer(4), Integer(5), Integer(1)], Integer(2): [Integer(2)], Integer(3): [Integer(3), Integer(6)]}) Graphics object consisting of 14 graphics primitives
- set_vertices(**vertex_options)[source]¶
Set the vertex plotting parameters for this
GraphPlot
.This function is called by the constructor but can also be called to make updates to the vertex options of an existing
GraphPlot
object. Note that the changes are cumulative.EXAMPLES:
sage: g = Graph({}, loops=True, multiedges=True, sparse=True) sage: g.add_edges([(0, 0, 'a'), (0, 0, 'b'), (0, 1, 'c'), ....: (0, 1, 'd'), (0, 1, 'e'), (0, 1, 'f'), ....: (0, 1, 'f'), (2, 1, 'g'), (2, 2, 'h')]) sage: GP = g.graphplot(vertex_size=100, edge_labels=True, ....: color_by_label=True, edge_style='dashed') sage: GP.set_vertices(talk=True) sage: GP.plot() Graphics object consisting of 22 graphics primitives sage: GP.set_vertices(vertex_color='green', vertex_shape='^') sage: GP.plot() Graphics object consisting of 22 graphics primitives
>>> from sage.all import * >>> g = Graph({}, loops=True, multiedges=True, sparse=True) >>> g.add_edges([(Integer(0), Integer(0), 'a'), (Integer(0), Integer(0), 'b'), (Integer(0), Integer(1), 'c'), ... (Integer(0), Integer(1), 'd'), (Integer(0), Integer(1), 'e'), (Integer(0), Integer(1), 'f'), ... (Integer(0), Integer(1), 'f'), (Integer(2), Integer(1), 'g'), (Integer(2), Integer(2), 'h')]) >>> GP = g.graphplot(vertex_size=Integer(100), edge_labels=True, ... color_by_label=True, edge_style='dashed') >>> GP.set_vertices(talk=True) >>> GP.plot() Graphics object consisting of 22 graphics primitives >>> GP.set_vertices(vertex_color='green', vertex_shape='^') >>> GP.plot() Graphics object consisting of 22 graphics primitives
Vertex labels are flexible:
sage: g = graphs.PathGraph(4) sage: g.plot(vertex_labels=False) Graphics object consisting of 4 graphics primitives
>>> from sage.all import * >>> g = graphs.PathGraph(Integer(4)) >>> g.plot(vertex_labels=False) Graphics object consisting of 4 graphics primitives
sage: g = graphs.PathGraph(4) sage: g.plot(vertex_labels=True) Graphics object consisting of 8 graphics primitives
>>> from sage.all import * >>> g = graphs.PathGraph(Integer(4)) >>> g.plot(vertex_labels=True) Graphics object consisting of 8 graphics primitives
sage: g = graphs.PathGraph(4) sage: g.plot(vertex_labels=dict(zip(g, ['+', '-', '/', '*']))) Graphics object consisting of 8 graphics primitives
>>> from sage.all import * >>> g = graphs.PathGraph(Integer(4)) >>> g.plot(vertex_labels=dict(zip(g, ['+', '-', '/', '*']))) Graphics object consisting of 8 graphics primitives
sage: g = graphs.PathGraph(4) sage: g.plot(vertex_labels=lambda x: str(x % 2)) Graphics object consisting of 8 graphics primitives
>>> from sage.all import * >>> g = graphs.PathGraph(Integer(4)) >>> g.plot(vertex_labels=lambda x: str(x % Integer(2))) Graphics object consisting of 8 graphics primitives
- show(**kwds)[source]¶
Show the (di)graph associated with this
GraphPlot
object.INPUT:
This method accepts all parameters of
sage.plot.graphics.Graphics.show()
.Note
See
the module's documentation
for information on default values of this method.Any options not used by plot will be passed on to the
show()
method.
EXAMPLES:
sage: C = graphs.CubeGraph(8) sage: P = C.graphplot(vertex_labels=False, vertex_size=0, ....: graph_border=True) sage: P.show()
>>> from sage.all import * >>> C = graphs.CubeGraph(Integer(8)) >>> P = C.graphplot(vertex_labels=False, vertex_size=Integer(0), ... graph_border=True) >>> P.show()