Ask Your Question

creating graph with dotted edges

asked 2018-10-08 18:17:43 +0200

GA316 gravatar image

Hello all, I need to draw a graph in which some edges are usual and some edges are dotted. I know how to make all the edges dotted (using the option "dotted" for the parameter edge_style of plot). But I need mixed edges (some are dotted and some are usual). Kindly help me with this. Thank you.

edit retag flag offensive close merge delete


1 Answer

Sort by ยป oldest newest most voted

answered 2018-10-08 20:41:48 +0200

tmonteil gravatar image

updated 2018-10-10 16:25:15 +0200

Let me pick a graph:

sage: G = graphs.RandomGNP(10, 1/2)

Together with two subsets of edges to be drawn differently:

sage: dotted_edges = G.edges()[:10]
sage: solid_edges = G.edges()[10:]

First, note that there is a simple way to color the edges according to the previous partition:

sage: G.plot(edge_colors = {'red': dotted_edges, 'green': solid_edges})

However, setting the edge_style in a non-uniform way is currently not possible (though it could be a good feature to request). A possible workaround is the following: we can build two graphs corresponding to the two (or more) subsets of edges, and add the plots:

sage: Graph(dotted_edges).plot(edge_style="dotted") + Graph(solid_edges).plot(edge_style="solid")

As you can see, this does not work correctly, since the position of the vertices is computed twice hence the vertices of the second graph are not at the same position than the corresponding vertices of the first graph. So what we have to do is to save the positions of some plot, and then to use them in the requested plot:

sage: G.plot(save_pos=True)
sage: position = G.get_pos()

sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid")

EDIT If you do not like axes, you can add axes=False to one of the plots:

sage: Graph(dotted_edges).plot(pos=position, edge_style="dotted") + Graph(solid_edges).plot(pos=position, edge_style="solid", axes=False)
edit flag offensive delete link more


Thanks. It works nicely. Can you please tell me how to add labels only on certain edges? thank you.

GA316 gravatar imageGA316 ( 2018-10-10 08:03:52 +0200 )edit

Also in the output graph with dotted edges and ordinary edges, I am getting axes also part of the output. But I need only the graph in order to add it in my paper. Kindly tell me how to get rid of this measurement axes. Thank yu.

GA316 gravatar imageGA316 ( 2018-10-10 08:17:11 +0200 )edit

@GA316: what do you mean by "add labels only on certain edges"?

slelievre gravatar imageslelievre ( 2019-04-21 13:04:48 +0200 )edit

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: 2018-10-08 18:17:43 +0200

Seen: 660 times

Last updated: Oct 10 '18