Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在双向布局中创建非双向图_Python_Graph_Networkx - Fatal编程技术网

Python 如何在双向布局中创建非双向图

Python 如何在双向布局中创建非双向图,python,graph,networkx,Python,Graph,Networkx,我有一个有很多节点的图。我将根据他们的时间步长将他们分开。假设我有3个时间步,每个时间步有5个节点。所以我想创建一个3列图,其中每列有5个节点。所以如果我们想象,它需要像一个二分布局。 但这不是一个二部图,因此我不知道如何继续 # Greate your Bipartite graph object G = nx.Graph() y_out = [6, 6, 12 ,8, 23, 23] # Add nodes to set 0 and 1 G.add_nodes_from(y_out, b

我有一个有很多节点的图。我将根据他们的时间步长将他们分开。假设我有3个时间步,每个时间步有5个节点。所以我想创建一个3列图,其中每列有5个节点。所以如果我们想象,它需要像一个二分布局。 但这不是一个二部图,因此我不知道如何继续

# Greate your Bipartite graph object
G = nx.Graph()


y_out = [6, 6, 12 ,8, 23, 23]
# Add nodes to set 0 and 1
G.add_nodes_from(y_out, bipartite=0)
G.add_nodes_from(['a6', 'b6', 'c12', 'd8', 'd23', 'e23'], bipartite=1)

# # Add edges between exclusive sets
# G.add_edges_from([dets_tuple])

top_nodes = {n for n, d in G.nodes(data=True) if d['bipartite'] == 0}
bottom_nodes = set(G) - top_nodes

print("top:", top_nodes)
print("bottom: " ,bottom_nodes)

# Visualize the graph
plt.subplot(121)
nx.draw_networkx(
    G, pos=nx.drawing.layout.bipartite_layout(G, top_nodes))
plt.show()
更新代码:

unique_frames = set(y_out[:, 0])
print("Generate dynamic graph")

# Visualize the graph
plt.subplot(121)

for i in unique_frames:

    # find indices that belong to the same frame
    idx = np.where(y_out[:, 0] == i)
    print("Comparing frame no: ", i)
    print("Idx : ", idx)
    # embed()

    curr_nodes = np.asarray(idx)[0]

    # Add nodes to set 0 and 1
    G.add_nodes_from(curr_nodes, bipartite=i)

    # compute partition nodes
    top_nodes = {n for n, d in G.nodes(data=True) if d['bipartite'] == i}

    # Draw Graph
    nx.draw_networkx(G, pos=nx.drawing.layout.bipartite_layout(G, top_nodes))

    plt.show(block=False)
    plt.pause(0.5)
这是一个二部图。我暂时插入了节点,这样它就遵守了二分规则。但理想情况下,我不能有一个。如何构造具有二部布局的正规多重连通图


更新:新更新的代码正在创建图形,但在同一列上。我不知道如何将它们水平隔开

“因此,如果我们想象一下,它需要像一个两部分布局。”Why@Acccumulation这更像是我的要求。很抱歉“它需要像一个二分布局”这样的错误沟通,你的意思是你想让节点根据时间步长分开吗?tilmestep中的节点之间是否有任何链接?对于这种类型的问题,发布一个小草图通常会有所帮助——手绘或ascii艺术往往效果很好,而且完成得很快。