我是 NetworkX 的新手,我想知道是否可以繪制字母而不是數字的節點?
目前我收到此錯誤。
NetworkXError: Node 'A' has no position
我正在使用“應該”處理數字的代碼。
我應該做哪些改變?我沒有找到有關此事的檔案。
謝謝!
import networkx as nx
# Nodes and edges
nodos = [("A"),("B"),("C"),("D"),("E"),("F"),("G"),("H"),("I"),("J"),("K")]
aristas= [("A","B"),("A","C"),("C","D"),("D","F"),("D","G"),("G","H"),("H","I"),("H","J"),("H","K"),("L","E")]
G = nx.Graph()
%matplotlib inline
graph_pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, graph_pos, nodos, node_size=400, node_color='blue', alpha = 0.6)
nx.draw_networkx_labels(G, graph_pos, font_size=12, font_family='sans-serif')
nx.draw_networkx_edges(G, graph_pos, edgelist=aristas, edge_color='green', alpha=0.5)
uj5u.com熱心網友回復:
您的代碼幾乎是正確的,但缺少節點和邊的實際添加:
# make sure to add the data to the graph
G.add_nodes_from(nodos)
G.add_edges_from(aristas)
這是完整的片段:
import networkx as nx
# Nodes and edges
nodos = [("A"), ("B"), ("C"), ("D"), ("E"), ("F"), ("G"), ("H"), ("I"), ("J"), ("K")]
aristas = [
("A", "B"),
("A", "C"),
("C", "D"),
("D", "F"),
("D", "G"),
("G", "H"),
("H", "I"),
("H", "J"),
("H", "K"),
("L", "E"),
]
G = nx.Graph()
# make sure to add the data to the graph
G.add_nodes_from(nodos)
G.add_edges_from(aristas)
graph_pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, graph_pos, nodos, node_size=400, node_color="blue", alpha=0.6)
nx.draw_networkx_labels(G, graph_pos, font_size=12, font_family="sans-serif")
nx.draw_networkx_edges(G, graph_pos, edgelist=aristas, edge_color="green", alpha=0.5)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/402801.html
標籤:
下一篇:使用Python進行財務資料分析
