Ask Your Question
1

Changing labels of a graph and access edge thickness

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

Cyrille gravatar image

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

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)
D.show(edge_colors={(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
1

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

tmonteil gravatar image

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

You can relabel the graph with the relabel method:

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

check

sage: D.relabel?

for more options.

Regarding coloring the vertices, you can do

sage: D.show(vertex_colors={'blue':[0,2],'green':[1],'black':[3]})

see

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:

sage: D.show(edge_thickness=4)

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

edit flag offensive delete link more

Comments

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 +0100 )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 +0100 )edit

Sorry but this

D = DiGraph( { 0: [1, 2, 3, 4], 1: [2, 3, 4], 2: [3, 4], 3: [4], 4:[]})
D.relabel(['A','B','C','D'])
vc = {'#51c8f1':[0,1,2,3,4]}
ec = {'#ad8393':[(0,1),(0,4)],'#4c75a3':[(2,4),(3,4)]}
D.edges(labels=False)

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 +0100 )edit

My version is sage 9.2

Cyrille gravatar imageCyrille ( 2021-01-05 16:48:27 +0100 )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 +0100 )edit
0

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

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

Stats

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

Seen: 1,112 times

Last updated: Jan 05 '21