How to plot on screen the entire nxgraph?
Closed this issue · 4 comments
I'm trying to debug my graph generated with PyMaxflow. I'd like to print it on screen, so I used the plot_graph
function found in the examples.
The problem is: it doesn't print both weights and the links with s and t nodes.
How can I achieve this?
Thank you very much
Hi.
The weights are available in the graph (in the attribute 'weight' of every edge). You can print that information using the draw_networkx_edge_labels
function from NetworkX
. You can also change the width of every edge passing a list of weights to the argument width
of networkx.draw
. Take a look at this example.
If you need more advanced drawing capabilities, you can export your graph with write_dot
and use GraphViz to draw it, or alternatively you can draw it using PyDot or PyGraphviz.
Regarding the links with 's' and 't' nodes, they are also available in the graph. In plot_graph
they are removed from the graph before plotting, but you can simply comment out the line where they are removed.
This modified plot_graph
might serve you as a starting point:
def plot_graph(nxgraph):
X, Y = np.mgrid[:5, :5]
aux = np.array([Y.ravel(), X[::-1].ravel()]).T
positions = {i:aux[i] for i in xrange(25)}
positions['s'] = (2,-1)
positions['t'] = (2,5)
edgewidth = []
for (u, v, d) in nxgraph.edges(data=True):
edgewidth.append(d['weight'])
# nxgraph.remove_nodes_from(['s', 't'])
plt.clf()
nx.draw(nxgraph, pos=positions, width=edgewidth)
# nx.draw_networkx_edge_labels(nxgraph, pos=positions)
plt.axis('equal')
plt.show()
Let me know if something is not clear.
Oh, perfect, it's very useful! Thank you very much :)
Just another question:
If I have non-symmetric edges, there is a way to print both on the graph? Actually it is overlapping them, so I can see only one of them...
You're welcome!
You can try this to print the weights as numbers. That way you can see the weights of both edges.
Unfortunately, if you want both edges are drawn separately, I think NetworkX drawing methods are too weak for that and you'll need to use other tools (e.g., [Py]Graphviz). However, I'm not an expert on NetworkX and you might find more details asking them.
Thank you very much, it was very useful!