简体   繁体   中英

Problems with networkx node 2 has no position

I'm sorry but I read through all similar questions and I couldn't find a solution to fix my problem.

import folderstats
import networkx as nx
from networkx.drawing.nx_pydot import graphviz_layout

df = folderstats.folderstats(
    '../', hash_name='md5',
    exclude=["tests", "venv", "__pycache__"],
    ignore_hidden=True)

# Sort the index
df_sorted = df.sort_values(by='id')
G = nx.Graph()
    for i, row in df_sorted.iterrows():
        if row.parent:
            G.add_edge(int(row.id), int(row.parent))

# Print some additional information

pos_dot = graphviz_layout(G, prog='dot',root=1)

fig = plt.figure(figsize=(16, 8))
nodes = nx.draw_networkx_nodes(G, pos_dot, node_size=2, node_color='C0')
edges = nx.draw_networkx_edges(G, pos_dot, edge_color='C0', width=0.5)
plt.axis('off')

What I always get is this:

NetworkXError: Node 2 has no position.

I'm looking forward to get your help! 🙂

Ok after several hours of testing, I found the solution.

First:

  • The graphviz_layout gives you a float -> Convert multiply by 100

    pos_dot = {k: (v[0]*100, v[1]*100) for k, v in pos_dot.items()}

Second:

  • It's still a float so you have to convert it to an integer

    pos_dot = {int(k): v for k, v in pos_dot.items()}

And that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM