【问题标题】:NetworkX - Connected ColumnsNetworkX - 连接的列
【发布时间】:2018-09-11 21:33:01
【问题描述】:

我正在尝试创建数据可视化,也许 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)

【问题讨论】:

  • 您是否考虑过为此简单地使用 matplotlib 并在点之间手动创建线条?文本注释需要一些技巧,但我认为这更容易。
  • 也许你也想考虑一下这个:stackoverflow.com/a/11809184/6673446
  • 检查draw_networkxpos参数。它需要一个节点的字典:位置,一个位置是一个简单的坐标元组。
  • @wolfrevokcats,谢谢!我不得不解决它,但这使我走上了正确的轨道,我发现这是一个可行的解决方案(在更新中)。想法?
  • @JamesGregorie,不,这样是错误的,你最好在函数中创建 pos 并返回:def create_pos(..): pos={} ... return pos。然后:pos1 = create_pos(0, nodes); pos2 = create_pos(1, nodes1)。最后合并:pos = { **pos1, **pos2 }

标签: visualization data-visualization networkx graph-layout


【解决方案1】:

这是我在@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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2018-05-06
    • 1970-01-01
    相关资源
    最近更新 更多