# creating graph with dotted edges

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 close merge delete

Sort by » oldest newest most voted

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)

more

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

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.