Ask Your Question

Changing labels of a graph and access edge thickness

asked 2021-01-05 12:27:03 +0200

Cyrille gravatar image

updated 2021-01-05 16:14:19 +0200

slelievre gravatar image

This is a graph. But now I want the vertices labeled with {'A', 'B', 'C'} or some names, and colored. And I have not seen an option for specifying the thickness of edges and of the arrow head.

Working with this example adapted from the documentation:

D = DiGraph({0: [0, 1], 1: [2], 2: [3]}, loops=True){(0, 1, 1): [(0, 1), (2, 3)]})

I have been surprised not to be allowed to put edge_colors inside show() in my case, but this is of less importance since there is another way.

Of more importance, in graph options I have not seen any command for the thickness of the edges and of the arrow head.

One more time thanks for your help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-01-05 12:51:49 +0200

tmonteil gravatar image

updated 2021-01-05 12:59:27 +0200

You can relabel the graph with the relabel method:

sage: D.relabel(['A','B','C','D'])


sage: D.relabel?

for more options.

Regarding coloring the vertices, you can do



sage: D.plot?

for more plotting options.

There might be one somewhere, but i do not see an edge-thickness option either in the documentation. However, looking into the source code of the GraphPlot class:

sage: from sage.graphs.graph_plot import GraphPlot
sage: GraphPlot??

It seems that you can do:


Also, there is a edge_style option which can be one of 'solid', 'dashed', 'dotted', dashdot', 'None'.

edit flag offensive delete link more


Thanks. But D.relabel() generates an error in my case. Also ?relabel() gives "Object relabel() not found."

Cyrille gravatar imageCyrille ( 2021-01-05 13:26:17 +0200 )edit

Which version of sagemath are you using and what's the error message ?

sage: D = DiGraph({0:[0,1], 1:[2], 2:[3]}, loops=True)                                                                                              
sage: D.relabel(['A','B','C','D'])                                                                                                                  
sage: D.edges(labels=False)                                                                                                                         
[('A', 'A'), ('A', 'B'), ('B', 'C'), ('C', 'D')]
David Coudert gravatar imageDavid Coudert ( 2021-01-05 13:32:37 +0200 )edit

Sorry but this

D = DiGraph( { 0: [1, 2, 3, 4], 1: [2, 3, 4], 2: [3, 4], 3: [4], 4:[]})
vc = {'#51c8f1':[0,1,2,3,4]}
ec = {'#ad8393':[(0,1),(0,4)],'#4c75a3':[(2,4),(3,4)]}

generates this

<repr(<sage.graphs.views.EdgesView at 0x6ffec68c7110>) failed: TypeError: '<' not supported between instances of 'int' and 'str'>
Cyrille gravatar imageCyrille ( 2021-01-05 14:32:26 +0200 )edit

My version is sage 9.2

Cyrille gravatar imageCyrille ( 2021-01-05 16:48:27 +0200 )edit

Because the vertices of your new graph are 'A', 'B', 'C', 'D', 4 and since 'A' and 4 are not comparable (a string vs an integer), apparently, you need to add sort=False option to prevent sorting :

sage: D.edges(labels=False, sort=False)
[('A', 'B'), ('A', 'C'), ('A', 'D'), ('A', 4), ('B', 'C'), ('B', 'D'), ('B', 4), ('C', 'D'), ('C', 4), ('D', 4)]

I would say that raising an error in such case is a kind of bug.

tmonteil gravatar imagetmonteil ( 2021-01-06 06:44:45 +0200 )edit

answered 2021-01-05 16:20:57 +0200

slelievre gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-01-05 12:27:03 +0200

Seen: 629 times

Last updated: Jan 05 '21