You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Networkx多层环形布局适配:固定内圈后计算外圈坐标问题

企业关联网络内外圈坐标对齐解决方案

问题分析

当前通过circular_layout分别生成内圈(子公司)和外圈(合作企业)坐标,导致内外圈节点无法对应。核心问题是外圈坐标未基于内圈子公司的位置计算,而是独立生成,自然无法保证同方向靠近。

解决思路

基于内圈子公司的坐标,将对应合作企业的坐标沿“中心-子公司”方向向外延伸至外圈尺度:

  • 保留已固定的内圈子公司坐标
  • 遍历所有合作企业,定位其关联的子公司节点
  • 对子公司坐标做归一化处理,再乘以外圈缩放比例,得到合作企业的对应位置,确保两者方向完全一致

修改后代码

import networkx as nx
import numpy as np

# 构建内圈(主企业+子公司)图及坐标
G1 = nx.from_pandas_edgelist(layer1_edges,
                           source='source',
                           target='target',
                           create_using=nx.MultiDiGraph())

pos1 = nx.circular_layout(G1, scale=200, center=(0, 0))
pos1["centered_node"] = np.array([0.0, 0.0])  # 主企业固定在中心

# 构建外圈(子公司+合作企业)图
G2 = nx.from_pandas_edgelist(layer2_edges,
                           source='source',
                           target='target',
                           create_using=nx.MultiDiGraph())

# 合并图
F = nx.compose(G1, G2)
pos_f = pos1.copy()

# 基于内圈坐标计算外圈合作企业的位置
outer_scale = 300  # 外圈缩放尺度
for u, v in G2.edges():
    # 判断子公司(已在pos1中)和合作企业(新增节点)
    if u in pos1 and v not in pos1:
        sub_node = u
        outer_node = v
    elif v in pos1 and u not in pos1:
        sub_node = v
        outer_node = u
    else:
        continue  # 跳过子公司间的边(若存在)
    
    # 获取子公司坐标并计算外圈位置
    sub_pos = pos1[sub_node]
    if np.linalg.norm(sub_pos) == 0:
        continue  # 跳过中心节点
    direction = sub_pos / np.linalg.norm(sub_pos)
    outer_pos = direction * outer_scale
    pos_f[outer_node] = outer_pos

效果说明

修改后,每个合作企业会精准出现在对应子公司的正外侧,完全实现内外圈同方向对齐的需求。

企业关联网络示意图

内容的提问来源于stack exchange,提问作者aearslan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 08:12:50