Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.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中使用networkx创建固定的节点集_Python_Numpy_Pandas_Matplotlib - Fatal编程技术网

在python中使用networkx创建固定的节点集

在python中使用networkx创建固定的节点集,python,numpy,pandas,matplotlib,Python,Numpy,Pandas,Matplotlib,我有一个关于图表的问题。我有30个节点(点)。我想以这样的方式构造一个邻接矩阵,每十组节点就像三角形的一个顶点。假设一组10个节点位于三角形ABC的顶点a,B和C 其中两个顶点集应该只有10条边(基本上簇内的每个节点都连接到另一个)。假设A和B的组在组内有10条边。而第三个顶点集应该有11条边(每个节点10条,一个节点与两个节点连接,因此该组中有11条边)。假设C处的有11条边 所有这三个簇之间都有一条边,形成一个三角形,即a组与B组之间有一条边,B组与C组之间有一条边,C组与a组之间有一条边

我有一个关于图表的问题。我有30个节点(点)。我想以这样的方式构造一个邻接矩阵,每十组节点就像三角形的一个顶点。假设一组10个节点位于三角形ABC的顶点a,B和C

其中两个顶点集应该只有10条边(基本上簇内的每个节点都连接到另一个)。假设A和B的组在组内有10条边。而第三个顶点集应该有11条边(每个节点10条,一个节点与两个节点连接,因此该组中有11条边)。假设C处的有11条边

所有这三个簇之间都有一条边,形成一个三角形,即a组与B组之间有一条边,B组与C组之间有一条边,C组与a组之间有一条边

稍后,我将在B和C之间再添加一条边。在附图中用虚线表示。顶点处的点可以是圆或任何其他形状,只要它们代表一个组

我如何为这样的东西创建一个邻接矩阵。实际上,我知道如何为这样一个矩阵创建邻接矩阵,因为它只是二元对称矩阵(无向图),但问题是,当我试图绘制邻接矩阵时,它会使另一个组中的一个节点更接近该节点所连接的组。假设我通过连接两个节点之间的边,将顶点A的一个节点与顶点B的一个节点连接起来。这条边将描绘三角形的边AB。但当我用networkx描述它时,从这两个不同的组连接起来的两个节点最终会更接近,看起来像是一个组的一部分。我如何将其作为单独的组保存

请注意,我正在使用python的networkx库来帮助绘制邻接矩阵

编辑:

我在以下灵感启发下尝试使用的代码:

G=nx.Graph()
# Creating three separate groups of nodes (10 nodes each)
node_clusters = [range(1,11), range(11,21) , range(21,31)]

# Adding edges between each set of nodes in each group. 
for x in node_clusters:
    for y in x: 
        if(y!=x[-1]):
            G.add_edge(y,y+1,len=2)
        else: 
            G.add_edge(y,x[0],len=2)
# Adding three inter group edges separately:
for x in range(len(node_clusters)):
    if(x<2):
        G.add_edge(node_clusters[x][-1],node_clusters[x+1][0],len=8)
    else:
        G.add_edge(node_clusters[x][-1],node_clusters[0][0],len=8)

nx.draw_graphviz(G, prog='neato')
我的python版本不是3,而是2。我用的是蟒蛇分布

编辑2:

我使用了马吕斯的代码,但使用了以下代码进行绘图:

graph_pos=nx.spring_layout(G,k=0.20,iterations=50)
nx.draw_networkx(G,graph_pos)
它完全破坏了整个图形。并表明:


我能很快地完成一些事情,只需修改一下,你所需要做的就是把表示每条边的元组放在一起,你还可以在边上设置一些任意长度,以获得理想布局的近似值:

import networkx
import string

all_nodes = string.ascii_letters[:30]
a_nodes = all_nodes[:10]
b_nodes = all_nodes[10:20]
c_nodes = all_nodes[20:]

all_edges = []
for node_set in [a_nodes, b_nodes, c_nodes]:
    # Link each node to the next
    for i, node in enumerate(node_set[:-1]):
        all_edges.append((node, node_set[i + 1], 2))
    # Finish off the circle
    all_edges.append((node_set[0], node_set[-1], 2))

joins = [(a_nodes[0], b_nodes[0], 8), (b_nodes[-1], c_nodes[0], 8), (c_nodes[-1], a_nodes[-1], 8)]

all_edges += joins
# One extra edge for C:
all_edges.append((c_nodes[0], c_nodes[5], 5))

G = networkx.Graph()
for edge in all_edges:
    G.add_edge(edge[0], edge[1], len=edge[2])

networkx.draw_graphviz(G, prog='neato')
如果要导出为邻接矩阵,请尝试类似于
networkx.to\u numpy\u matrix(G)


是否也可以将其作为一个圆形?每个顶点?如果你谈论的是它实际上是如何绘制的,你可以使用GraphViz,它有一些更好的布局算法,例如
networkx.draw\u GraphViz(g,prog='neato')
。但是,您必须使用Python 2,
pygraphviz
对于Python 3不存在。谢谢!networkx中有没有一种方法可以给我这个G的邻接矩阵?二进制对称矩阵?@Manish查看我的编辑,不确定您要查找的格式,但您可以执行类似于
networkx.to\u numpy\u matrix(G)
的操作。我还改进了布局,增加了每条边的长度。嗨,马吕斯。这很酷。我从您的代码中获得了灵感(不想公然复制),并编写了我的代码。以上编辑。我无法理解代码中使用的8和2。是边长吗?。那么为什么len=edge[2]?。另外,当我试图绘制图表时,我也遇到了一个错误。见上文
import networkx
import string

all_nodes = string.ascii_letters[:30]
a_nodes = all_nodes[:10]
b_nodes = all_nodes[10:20]
c_nodes = all_nodes[20:]

all_edges = []
for node_set in [a_nodes, b_nodes, c_nodes]:
    # Link each node to the next
    for i, node in enumerate(node_set[:-1]):
        all_edges.append((node, node_set[i + 1], 2))
    # Finish off the circle
    all_edges.append((node_set[0], node_set[-1], 2))

joins = [(a_nodes[0], b_nodes[0], 8), (b_nodes[-1], c_nodes[0], 8), (c_nodes[-1], a_nodes[-1], 8)]

all_edges += joins
# One extra edge for C:
all_edges.append((c_nodes[0], c_nodes[5], 5))

G = networkx.Graph()
for edge in all_edges:
    G.add_edge(edge[0], edge[1], len=edge[2])

networkx.draw_graphviz(G, prog='neato')