简体   繁体   中英

Node has no position in Networkx

The code is trying to plot a networkx Graph, and add colors on node, however, it told me that error on NetworkXError: Node 'Contact2' has no position .

import pandas as pd
%matplotlib notebook
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

G = nx.DiGraph()
G.add_edge('Owner','Contact1')
G.add_edge('Owner','Conatct2')
G.add_edge('Contact1','Firm1')
G.add_edge('Conatct2','Firm2')
G.add_edge('Firm1','Org1')
G.add_edge('Firm1','Org2')
G.add_edge('Firm2','Org3')

pos=nx.spring_layout(G)
nx.draw_networkx(G)
nx.draw_networkx_nodes(G,pos,
                           nodelist=['Owner'],
                           node_color='k',
                           node_size=500,
                       alpha=0.8)
nx.draw_networkx_nodes(G,pos,
                           nodelist=['Contact1','Contact2'],
                           node_color='r',
                           node_size=500,
                       alpha=0.8)
nx.draw_networkx_nodes(G,pos,
                           nodelist=['Firm1','Firm2'],
                           node_color='#8B8378',
                           node_size=500,
                       alpha=0.8)
nx.draw_networkx_nodes(G,pos,
                           nodelist=['Org1','Org2','Org3'],
                           node_color='r',
                           node_size=500,
                       alpha=0.8)

You just have a misspelling:

G.add_edge('Owner','Conatct2')

You have a Conatct2 node. You should replace it with Contact2 .

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