首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >NetworkX连接列

NetworkX连接列
EN

Stack Overflow用户
提问于 2018-04-02 08:40:21
回答 1查看 268关注 0票数 0

我正在尝试创建一个数据可视化,也许NetworkX不是最好的工具,但我希望有连接彼此的节点的并行列(两个单独的组)。我不知道如何在这个布局中放置两个节点组。我尝试过的不同选项总是默认为更“类似于网络”的布局。我正在尝试创建一个可视化,其中客户/公司(字典中的键)将被绘制到产品节点(同一字典中的值)的边缘。

例如:

代码语言:javascript
运行
AI代码解释
复制
d = {"A":[ 1, 2, 3], "B": [2,3], "C": [1,3]

在字典'd‘中,我们将有一列节点"A“、"B”、"C“和第二列1、2、3,在这两个边之间绘制。

代码语言:javascript
运行
AI代码解释
复制
A                   1
B                   2
C                   3

更新:

因此,'pos‘的论点是有帮助的,但我认为它在多个对象上使用它有困难。下面是我想出的方法:

代码语言:javascript
运行
AI代码解释
复制
nodes = ["A", "B", "C", "D"]
nodes2 = ["X", "Y", "Z"]

edges = [("A","Y"),("C","X"), ("C","Z")]

#This function will take a list of values we want to turn into nodes
# Then it assigns a y-value for a specific value of X creating columns
def create_pos(column, node_list):
    pos = {}
    y_val = 0
    for key in node_list:   
        pos[key] = (column, y_val)
        y_val = y_val+1
    return pos 



G.add_nodes_from(nodes)
G.add_nodes_from(nodes2)
G.add_edges_from(edges)

pos1 = create_pos(0, nodes)
pos2 = create_pos(1, nodes2)
pos = {**pos1, **pos2}

nx.draw(G, pos)
EN

回答 1

Stack Overflow用户

发布于 2018-04-04 08:47:58

下面是我在@wolfevokcats的帮助下编写的代码,用于创建连接的节点列。

代码语言:javascript
运行
AI代码解释
复制
G = nx.Graph()

nodes = ["A", "B", "C", "D"]
nodes2 = ["X", "Y", "Z"]

edges = [("A","Y"),("C","X"), ("C","Z")]

    #This function will take a list of values we want to turn into nodes
    # Then it assigns a y-value for a specific value of X creating columns
def create_pos(column, node_list):
    pos = {}
    y_val = 0
    for key in node_list:   
        pos[key] = (column, y_val)
        y_val = y_val+1
    return pos 



G.add_nodes_from(nodes)
G.add_nodes_from(nodes2)
G.add_edges_from(edges)

pos1 = create_pos(0, nodes)
pos2 = create_pos(1, nodes2)
pos = {**pos1, **pos2}

nx.draw(G, pos, with_labels = True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49615132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档