基于已确定节点与边的NetworkX绘图报错(TypeError)修复求助
Resolving TypeError in NetworkX add_edge() for Node-Edge Graph
Problem Context
You're trying to create a graph with NetworkX and Matplotlib to visualize your specified nodes and edges, but hitting a TypeError because the add_node() and add_edge() methods aren't being used correctly. Here's your original code, error message, and defined node/edge data:
Original Code
import matplotlib.pyplot as plt import networkx as nx %matplotlib notebook G = nx.Graph() G.add_node(y[i]==1, color="blue", size=500) G.add_node(y[i]==0, color="green", size=300) G.add_edge(d[k,i]*w[k,i], color="green") G.add_edge(d[i,j]*f[i,j]>0, color="blue") nx.draw(G, with_labels=True) plt.show()
Error Message
TypeError Traceback (most recent call last) <ipython-input-314-9036a4e704aa> in <module> 23 G.add_node(y[i]==1, color="blue", size=500) 24 G.add_node(y[i]==0, color="green", size=300) ---> 25 G.add_edge(d[k,i]*w[k,i], color="green") 26 G.add_edge(d[i,j]*f[i,j]>0, color="blue") 27 TypeError: add_edge() missing 1 required positional argument: 'v_of_edge'
Defined Node/Edge Data
- Nodes (Y): Nodes with value 1 are indices
0, 1, 3, 12, 57, 59 - Edges (W): Pairs
(22,0), (23,0), (24,0) - Edges (F): Pairs
(0,0), (0,1), (1,3), (3,59), (3,98), (8,51), (24,8), (25,0), (25,24), (51,8), (51,59), (57,12), (59,57)
Solution
The core issue is misuse of NetworkX's node/edge methods:
add_node()needs a unique node identifier (like the integer index), not a boolean condition.add_edge()requires two node identifiers (start and end of the edge), not a calculated value or boolean.
Here's the corrected code with step-by-step explanations:
Corrected Full Code
import matplotlib.pyplot as plt import networkx as nx %matplotlib notebook # Initialize the graph G = nx.Graph() # --- Add Nodes --- # Collect all relevant nodes (Y=1 nodes + nodes from edges) all_nodes = {0,1,3,12,57,59,22,23,24,8,25,51,98} # Add each node with color and size attributes for node in all_nodes: if node in {0,1,3,12,57,59}: # Match Y[node] == 1 G.add_node(node, color="blue", size=500) else: # Assume Y[node] == 0 for edge-only nodes G.add_node(node, color="green", size=300) # --- Add Edges --- # Add W edges (green color) w_edges = [(22,0), (23,0), (24,0)] for u, v in w_edges: G.add_edge(u, v, color="green") # Add F edges (blue color) f_edges = [(0,0), (0,1), (1,3), (3,59), (3,98), (8,51), (24,8), (25,0), (25,24), (51,8), (51,59), (57,12), (59,57)] for u, v in f_edges: G.add_edge(u, v, color="blue") # --- Prepare plotting attributes --- # Extract node colors, sizes, and edge colors from the graph node_colors = [G.nodes[node]['color'] for node in G.nodes] node_sizes = [G.nodes[node]['size'] for node in G.nodes] edge_colors = [G.edges[edge]['color'] for edge in G.edges] # --- Draw and display the graph --- nx.draw(G, with_labels=True, node_color=node_colors, node_size=node_sizes, edge_color=edge_colors) plt.show()
Key Fixes Explained
- Node Addition: We add nodes using their actual index values (not boolean checks) and set attributes based on whether they're in your Y=1 list. We also include all nodes that appear in edges to avoid missing nodes in the plot.
- Edge Addition: Each edge is added as a pair of node indices (
u, v), which is the required format foradd_edge(). We group W and F edges separately to assign their intended colors. - Plot Customization: We extract the node and edge attributes we set and pass them directly to
nx.draw()to ensure your styling (colors, sizes) is applied correctly.
内容的提问来源于stack exchange,提问作者Dominik
相关产品推荐
相关产品推荐

