Issue
If I make a tree using networkx and draw it, the nodes overlap. Is there a way to draw it so there is no overlap?
import matplotlib.pyplot as plt
import networkx as nx
T = nx.generators.balanced_tree(2, 5)
nx.draw(T)
plt.show()
Solution
I am no expert in this, but here is code that uses the pydot
library and its graph_viz
dependency. These libraries come with Anaconda Python but are not installed by default, so first do this from the command prompt:
conda install pydot
Then here is code adapted from Circular Tree.
import matplotlib.pyplot as plt
import networkx as nx
import pydot
from networkx.drawing.nx_pydot import graphviz_layout
T = nx.balanced_tree(2, 5)
pos = graphviz_layout(T, prog="twopi")
nx.draw(T, pos)
plt.show()
If you adjust the window to make it square, the result is
Or, if you prefer a top-down tree, you could replace the string "twopi"
in that code with "dot"
, and if you make the resulting window wider you get
Also, if you use the string "circo"
instead and make the window wider, you get
Answered By - Rory Daulton
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.