简体   繁体   English

python在SPYDER中绘制有向图

[英]python drawing directed graph in SPYDER

I am using SPYDER. 我正在使用SPYDER。 I have below code. 我有以下代码。 The code produces the graph. 代码生成图形。 But the graph is not clean. 但图表不干净。 I would like to see layers in the graph - my first layer has 'start' node, second layer has 1,2,3 node, third layer has 'a', 'b', 'c' nodes and the last layer has 'end' node. 我想在图中看到图层 - 我的第一层有'开始'节点,第二层有1,2,3节点,第三层有'a','b','c'节点,最后一层有'结束'节点。 Any idea how could i achieve my objective? 任何想法我怎么能实现我的目标?

import networkx as nx
import matplotlib.pyplot as plt
import os.path as path

G=nx.DiGraph()
G.add_nodes_from(['start',1,2,3,'a','b','c','end'])

G.nodes(data=True)

G.add_edge('start',2)
G.add_edge('start',3)
G.add_edge(2,'b')
G.add_edge('b','end')
G.add_edge('a','end')
G.add_edge('f','g')
nx.draw(G)

------------------------update------------------ ------------------------更新------------------

by layer i mean I should see the network like in the attached figure. 逐层我的意思是我应该看到附图中的网络。 I want the network drawn that way because a node in layer X will be connected directly ONLY to nodes in layer X+1 or layer X-1 我希望以这种方式绘制网络,因为层X中的节点将直接连接到层X + 1或层X-1中的节点

在此输入图像描述

It [seems that networkx does not provide much for arranging graphs but I have only touched it a few times so I can't say for sure. 它[似乎networkx没有为排列图表提供太多,但我只触及了几次,所以我不能肯定地说。 I couldn't find a way to do what you are asking. 我找不到办法去做你要问的事。

However, I tried using graphviz (which has to be installed first) to accomplish it. 但是,我尝试使用graphviz (必须先安装)才能完成它。 Credit for the ranking method goes to this SO answer . 排名方法的功劳归于此SO答案

Copy-Paste Example 复制粘贴示例

import networkx as nx

ranked_node_names = [['start'],
                     [1, 2, 3],
                     ['a', 'b', 'c'],
                     ['end']]
node_edges = [('start', 2),
              ('start', 3),
              (2, 'b'),
              ('b', 'end'),
              ('a', 'end')]

# graph and base nodes/edges in networkx
G = nx.DiGraph()
for rank_of_nodes in ranked_node_names:
    G.add_nodes_from(rank_of_nodes)
G.nodes(data=True)
G.add_edges_from(node_edges)
# I don't know a way to automatically arrange in networkx so using graphviz
A = nx.to_agraph(G)
A.graph_attr.update(rankdir='LR')  # change direction of the layout
for rank_of_nodes in ranked_node_names:
    A.add_subgraph(rank_of_nodes, rank='same')
# draw
A.draw('example.png', prog='dot')

Default Output 默认输出

在此输入图像描述

LR (left-right) Output LR(左 - 右)输出

在此输入图像描述

You have at least two options: You can create a dictionary with node positions yourself if you like. 您至少有两个选项:如果您愿意,可以自己创建一个包含节点位置的字典。 First assign nodes to layers and then: 首先将节点分配给图层,然后:

position = dict()
for (i, layer) in enumerate(layers):
    for (j, node) in enumerate(layer):
        position[node] = (i, j / float(len(layer))

and then you draw the network with the given node positions nx.draw(G, pos=position) . 然后用给定的节点位置nx.draw(G, pos=position)绘制网络。

The other option is to use graphviz and the python AGraph class which can be used to produce hierarchical layouts. 另一个选择是使用graphviz和python AGraph类,它可用于生成分层布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM