Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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_Matplotlib_Networkx - Fatal编程技术网

Python 从邻接矩阵将边权重添加到networkx中的打印输出

Python 从邻接矩阵将边权重添加到networkx中的打印输出,python,matplotlib,networkx,Python,Matplotlib,Networkx,我正在生成一个随机图,并从邻接矩阵中绘制它。我需要能够添加边权重 我看了看,这似乎工作得很好,这正是我在显示器上寻找的,但它只在单独添加边时起作用 我正在使用: nx.from_numpy_矩阵(G,使用=nx.DiGraph()创建_) 根据,如果非对称邻接矩阵只有整数项(确实如此),这些项将被解释为连接顶点的加权边(不创建平行边)。因此,在查看时,它们会抓取节点属性,抓取标签属性,然后绘制边标签。但我无法获取属性。有人知道如何在仍然使用这个邻接矩阵的情况下显示这些边吗 提前谢谢 from r

我正在生成一个随机图,并从邻接矩阵中绘制它。我需要能够添加边权重

我看了看,这似乎工作得很好,这正是我在显示器上寻找的,但它只在单独添加边时起作用

我正在使用: nx.from_numpy_矩阵(G,使用=nx.DiGraph()创建_)

根据,如果非对称邻接矩阵只有整数项(确实如此),这些项将被解释为连接顶点的加权边(不创建平行边)。因此,在查看时,它们会抓取节点属性,抓取标签属性,然后绘制边标签。但我无法获取属性。有人知道如何在仍然使用这个邻接矩阵的情况下显示这些边吗

提前谢谢

from random import random
import numpy
import networkx as nx
import matplotlib.pyplot as plt

#here's how I'm generating my random matrix
def CreateRandMatrix( numnodes = int):
    def RandomHelper():
        x = random()
        if x < .70:
            return(0)
        elif .7 <= x and x <.82:
            return(1)
        elif .82 <= x and x <.94:
            return(2)
        else:
            return(3)
    randomatrix = numpy.matrix([[RandomHelper() for x in range(numnodes)] for y in range(numnodes)])
    for i in range(len(randomatrix)):
        randomatrix[i,i]=0
    return randomatrix

#this generate the graph I want to display edge weights on
def Draw(n = int): 
    MatrixtoDraw = CreateRandMatrix(n)
    G = nx.from_numpy_matrix(MatrixtoDraw, create_using = nx.DiGraph())
    nx.draw_spring(G, title="RandMatrix",with_labels=True)
    plt.show()
如果我在空闲时单独运行每条线路,我会

>>> nx.get_node_attributes(G,'pos')
{}
>>> nx.get_node_attributes(G,'weight')
{}

为什么不从邻接矩阵生成的图形信息中获取它们?

在问题中显示答案(问题是什么?)没有什么意义。相反,请通过提供答案来回答你的问题。在问题中显示答案(问题是什么?)没有什么意义。相反,请通过提供答案来回答您的问题。
>>> nx.get_node_attributes(G,'pos')
{}
>>> nx.get_node_attributes(G,'weight')
{}