Đồ thị mạngX

Trực quan hóa các nút và cạnh thông quaMatplotlib.

Thử trong Trình chỉnh sửa

Tổng quan

Bản đồ thuật toán NetworkX đẩy các nút được kết nối với nhau xuống mảng tọa độ.

Các chức năng ánh xạ liên kết chính xác tới các điểm cuối matplotlib bên trong bộ nhớ trình duyệt.

Đầu ra mã & thực thi

Tạo một cây biểu đồ được kết nối ngẫu nhiên được vẽ nguyên bản.

import networkx as nx
import matplotlib.pyplot as plt

# Create a graph
G = nx.erdos_renyi_graph(n=12, p=0.3, seed=42)

# Calculate centrality
centrality = nx.degree_centrality(G)
node_sizes = [v * 3000 for v in centrality.values()]
node_colors = list(centrality.values())

fig, ax = plt.subplots(figsize=(8, 6))
pos = nx.spring_layout(G, seed=42)

# Draw graph
nodes = nx.draw_networkx_nodes(
    G, pos, 
    node_size=node_sizes, 
    node_color=node_colors, 
    cmap=plt.cm.viridis,
    edgecolors='white',
    linewidths=2
)
nx.draw_networkx_edges(G, pos, edge_color='gray', alpha=0.5, width=1.5)
nx.draw_networkx_labels(G, pos, font_color='white', font_family='sans-serif', font_weight='bold')

plt.title("Network Connectedness", fontsize=14, pad=15)
plt.axis('off')
plt.show()
Đầu ra thiết bị đầu cuối
Rendered Network Graph in the Graphs tab...

Triển khai từng bước

  • Lý thuyết thuật toán
  • Kết xuất nút xã hội
  • Truy vấn tôpô

Câu hỏi thường gặp

Chủ đề liên quan