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

สร้างขอบโค้งมนด้วย NetworkX ใน Python3 (Matplotlib)

ในการสร้างขอบโค้งมน (curved edges) ด้วย NetworkX ใน Python3 เราสามารถใช้ connectionstyle="arc3, rad=0.4"

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
  • เริ่มต้นกราฟพร้อม edges, name และ graph attributes
  • เพิ่ม nodes ให้กับกราฟที่สร้าง
  • เพิ่ม edges จากโหนดหนึ่งไปอีกโหนด
  • วาดกราฟ G ด้วย Matplotlib พร้อม connectionstyle="arc3, rad=0.4"
  • เพื่อแสดงรูป ใช้เมธอด show()

ตัวอย่าง

import matplotlib.pylab as plt
import networkx as nx

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

G = nx.DiGraph()
pos = nx.spring_layout(G)
G.add_nodes_from([1, 2, 3, 4])
G.add_edges_from([(1, 2), (2, 4), (2, 3), (4, 1)])
nx.draw(G, with_labels=True, connectionstyle="arc3,rad=0.4")
plt.show()

ผลลัพธ์

สร้างขอบโค้งมนด้วย NetworkX ใน Python3 (Matplotlib)