Reputation: 322
Is there some way, to assign pos value to a nx.Graph(), so that it will be drawn using this value by default when nx.draw() is called?
I want to store graphs in JSON format, and it would really help me if I would not have to store this information separately from the graph.
For what I know it is possible to use add_node() with pos parameter, to add single node at some location, but can I assign positions to nodes already present in Graph?
Upvotes: 1
Views: 310
Reputation: 322
After some though I realized that nx.set_node_attributes() can be used. The solution is then something like:
pos = nx.spring_layout(graph)
nx.set_node_attributes(graph, pos, "pos")
And later drawing graph with:
nx.draw(graph, pos=nx.get_node_attributes(graph,'pos'))
The last thing is I had some difficulties with saving the graph to JSON after adding those attributes, but it was also easily solvable.
Upvotes: 2