python - 如何在粗线上获得非圆形箭头?

标签 python networkx

我正在使用 networkx 在 Python 中绘制有向图。我使用不同宽度的边缘来突出重量。不幸的是,箭头是圆形的,看起来很奇怪。我想为看起来像细线箭头的缩放版本的粗线绘制非圆形箭头。

directed graph with weird fat arrows

import networkx as nx
import matplotlib.pyplot as plt
import numpy

G=nx.DiGraph()
stake = [
    [0, 1, 1, 3],
    [3, 0, 0, 1],
    [0, 0, 0, 1],
    [1, 0, 3, 0]
    ]
maxS = max(max(stake))
stake1d = numpy.concatenate(stake)
minS = min(stake1d[numpy.nonzero(stake1d)])
minLineWeight = 1
maxLineWeight = 10

for x in range(len(stake)):
    for y in range(x):
        if(stake[x][y] > 0):
            weight = (stake[x][y] - minS) / (maxS - minS) * (maxLineWeight - minLineWeight) + minLineWeight
            G.add_edge(x, y, weight=weight, color='r')

for x in range(len(stake)):
    for y in [i for i in range(len(stake))][-(len(stake)-x):]:
        if(stake[x][y] > 0):
            weight = (stake[x][y] - minS) / (maxS - minS) * (maxLineWeight - minLineWeight) + minLineWeight
            G.add_edge(x, y, weight=weight, color='b')

weights=list(nx.get_edge_attributes(G,'weight').values())
colors=list(nx.get_edge_attributes(G,'color').values())
pos = nx.shell_layout(G)
nx.draw(
    G,
    pos=pos,
    width=weights,
    edge_color=colors,
    with_labels=True,
    arrows=True,
    connectionstyle='arc3',
    arrowstyle='->'
)  
plt.show()

最佳答案

我有这个完全相同的问题并一起破解了一个解决方法:

  1. nx.draw 调用替换为两个单独的方法调用,分别绘制节点和边。这很有用,因为 networkx 边绘制方法 draw_networkx_edges 返回一个 matplotlib 补丁列表(特别是 FancyArrowPatch 对象),然后您可以在渲染之前对其进行操作。

  2. 遍历箭头补丁并更改 FancyArrowPatch mutation_scale ,以及 joinstylecapstyle构成每个箭头的线段。

解决方法

从您的示例代码开始,将对 nx.draw 的调用替换为以下内容。

# First, draw the nodes themselves
nodes = nx.draw_networkx_nodes(
    G,
    pos=pos,
    linewidths=1
)

# Draw node labels
node_label_handles = nx.draw_networkx_labels(
    G, 
    pos=pos
);

# Draw the edges and store the returned FancyArrowPatch list
arrows = nx.draw_networkx_edges(
    G,
    pos=pos,
    arrows=True,
    width=weights,
    edge_color=colors,
    arrowstyle='-|>'  # I personally think this style scales better
)

for a, w in zip(arrows, weights):
    
    # mutation_scale affects only the arrowhead size, not the arrow tail.
    # The constants here are arbitrary; you may want/need to change them
    a.set_mutation_scale(20 + w)
    
    # Sharpen arrowheads by creating a mitered joint between arrowhead 
    # line segments, instead of the default joinstyle='round'
    a.set_joinstyle('miter')
    
    # Prevent each arrow tail from jutting forward and through the arrowhead,
    # which happens with the default capstyle='projecting'
    a.set_capstyle('butt')

我的结果

plot result after adjusting arrow formatting

版本

pandas=1.3.2, numpy=1.21.2, matplotlib=3.4.3

关于python - 如何在粗线上获得非圆形箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67251763/

相关文章:

python - 即使由于 dagbag 错误而从 UI 手动触发了 dags,Airflow 也不会触发 dags

python - networkx 没有给出加权图的所有最短路径

python-3.x - Networkx 2.1+中修改单个节点属性的简单方法

python - 从neo4j查询结果构建NetworkX图?

python - 为什么相对路径在 python 测试中不起作用?

python - Psycopg2 使用外键创建记录

python - 更新 pandas 数据框(具有复杂条件的元素的部分总和)的最快方法

python - 如何在unittest中捕获python子进程stdout

python - networkx 删除节点属性

python - 在 Networkx 1.10 中,configuration_model() 获得了关键字参数 'create_using' 的多个值