簡體   English   中英

我如何在networkX圖中彎曲邊緣

[英]How do I curve edges in networkX graph

我之前曾問過這個關於如何在 networkX 中實現彎曲邊緣的問題。 我以前的數據工作正常,但是當我更新數據和代碼時,我不確定我哪里出錯了。 邊僅針對某些節點彎曲,並且實際上添加了兩次連接。 我不知道為什么它要繪制兩次邊緣,一次是直線,一次是曲線。 在此處輸入圖片說明

我的代碼:

import matplotlib.pyplot as plt
import networkx as nx

G = nx.Graph()

G.add_edge("Ted", "May", weight=0.5)
G.add_edge("Ted", "Ray", weight=1)
G.add_edge("Ted", "Chris", weight=1)
G.add_edge("Ted", "Sam", weight=3)
G.add_edge("Ted", "April", weight=1)
G.add_edge("Ted", "Ana", weight=0)


G.add_edge("Ana", "Ryan", weight=1)
G.add_edge("Ana", "Jim", weight=0.5)
G.add_edge("Ana", "Ben", weight=1)


for0 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 0]
for05 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 0.5]
for1 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 1]
for15 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 1.5]
for3 = [(u, v) for (u, v, d) in G.edges(data=True) if d["weight"] == 3]



pos = nx.circular_layout(G)  # positions for all nodes
ax=plt.gca()

# nodes
sc = nx.draw_networkx_nodes(G, pos, node_size=700)

# edges


nx.draw_networkx_edges(G, pos, edgelist=for0, width=0)
nx.draw_networkx_edges(G, pos, edgelist=for05, width=0.5)

nx.draw_networkx_edges(G, pos, edgelist=for1, width=1)
nx.draw_networkx_edges(G, pos, edgelist=for15, width=1.5)

nx.draw_networkx_edges(G, pos, edgelist=for3, width=3)

for edge in G.edges():
    source, target = edge
    rad = 0.2
    arrowprops=dict(arrowstyle="-",
                    color='blue',
                    connectionstyle=f"arc3,rad={rad}",
                    linestyle= '-',
                    alpha=0.6,)
    ax.annotate("",
                xy=pos[source],
                xytext=pos[target],
                arrowprops=arrowprops
               )

# labels
nx.draw_networkx_labels(G, pos, font_size=20, font_family="sans-serif")


plt.show()

直邊來自nx.draw_networkx_edges()調用。 如果刪除它們,則只剩下彎曲的邊緣,但它們沒有指定的邊緣權重。 您可以按如下方式更新 for 循環以獲取具有邊緣權重的彎曲邊緣。

for edge in G.edges():
    source, target = edge
    rad = 0.2
    arrowprops=dict(lw=G.edges[(source,target)]['weight'],
                    arrowstyle="-",
                    color='blue',
                    connectionstyle=f"arc3,rad={rad}",
                    linestyle= '-',
                    alpha=0.6)
    ax.annotate("",
                xy=pos[source],
                xytext=pos[target],
                arrowprops=arrowprops
               )

lw參數根據圖中的“權重”邊屬性設置線寬。 如果這不是您想要的,您可以將其設置為某個默認值或將其刪除。

帶加權曲線邊的圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM