Ask Your Question

Revision history [back]

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")

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)