Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีวาด Node Colormap ใน NetworkX/Matplotlib

ในการวาด node colormap ใน matplotlib/networkx เราสามารถทำตามขั้นตอนต่อไปนี้:

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • คืนกราฟวัฏจักร $C_n$ ของโหนดที่เชื่อมต่อแบบวนรอบ
  • จัดตำแหน่งโหนดบนวงกลม
  • วาดกราฟ G ด้วย Matplotlib
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import matplotlib.pyplot as plt
import networkx as nx

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

G = nx.cycle_graph(24)
pos = nx.circular_layout(G)
nx.draw(G, pos, node_color=range(24), node_size=800, cmap='copper')
plt.show()

ผลลัพธ์

วิธีวาด Node Colormap ใน NetworkX/Matplotlibวิธีวาด Node Colormap ใน NetworkX/Matplotlib