【问题标题】:Networkx curve edges to the middle/centerNetworkx曲线边缘到中间/中心
【发布时间】:2021-01-21 12:14:38
【问题描述】:

我正在使用具有 circular layout 的 Networkx 图。我想在彼此靠近的节点之间弯曲边缘以循环到图的中间并返回。 像这样的:

我正在使用此代码来弯曲边缘并更改半径值,但无论我应用什么半径,我都没有得到我想要的那么大的曲线。

for edge in G.edges():
    source, target = edge
    rad = 0.35
    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
               )

使用上述代码的相邻节点之间的边缘如下所示:

【问题讨论】:

    标签: python matplotlib networkx


    【解决方案1】:

    尝试明确指定pos,然后不断增加rad。 在下面的独立示例中,如图所示,半径肯定在增加。 (我已经使权重和半径依赖于图示的节点)

    G = nx.path_graph(10)
    pos = nx.circular_layout(G)
    
    nx.draw(G,pos,node_size=60,font_size=8) 
    for edge in G.edges():
        source, target = edge
        G.edges[(source,target)]['weight'] = max(source, target)
        rad = max(source, target)/4
        arrowprops=dict(lw=G.edges[(source,target)]['weight'],
                        arrowstyle="-",
                        color='blue',
                        connectionstyle=f"arc3,rad={rad}",
                        linestyle= '-',
                        alpha=0.6)
        ax = plt.gca()
        ax.annotate("",
                    xy=pos[source],
                    xytext=pos[target],
                    arrowprops=arrowprops
                   )
    

    希望这可以帮助您继续前进。

    【讨论】:

    • stackoverflow.com/questions/64193989/… - 感谢您的帮助。这就是我的代码的样子,我该如何满足您的代码?因为我没有使用路径图
    • 我收到此错误rad = max(source, target)/4 TypeError: unsupported operand type(s) for /: 'str' and 'int'
    • 错误告诉您源或目标是string 类型。请将其转换为整数,以便 max() 除以 4 起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-04-16
    • 2023-02-25
    • 2022-10-24
    相关资源
    最近更新 更多