0

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

例如:

d = {"A":[ 1, 2, 3], "B": [2,3], "C": [1,3]

从字典 'd' 中,我们将有一列节点 ["A", "B", "C"] 和第二列 [1, 2, 3] 并在两条边之间绘制。

A                   1
B                   2
C                   3

更新:

所以建议的“pos”参数有所帮助,但我认为在多个对象上使用它有困难。这是我想出的方法:

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)
4

1 回答 1

0

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

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)
于 2018-04-04T16:47:58.623 回答