简体   繁体   English

使用python igraph标记图形中的边缘

[英]Labelling the edges in a graph with python igraph

I have the adjacency matrix (ie a collection of weights) of a directed graph, and I would like to add labels (corresponding to the values of the weights) on the edges in the final plot. 我有一个有向图的邻接矩阵(即权重集合),我想在最终图中的边上添加标签(对应于权重的值)。 In other words, I would like to obtain something like this . 换句话说,我想获得像这样 I'm using python igraph, and my code is as follows: 我正在使用python igraph,我的代码如下:

import numpy as np
import igraph as ig


N = 6

adj_matr = np.random.random((N, N))

g = ig.Graph.Weighted_Adjacency(adj_matr.tolist(), mode=ig.ADJ_DIRECTED, attr="weight", loops=True)

ig.plot(g, "My_Graph.svg", vertex_label=map(str, np.arange(N)))

I have figured out how to set labels on the nodes, but I cannot find anything concrete about the edges (adding edge_label=... in the plot command doesn't work). 我已经想出如何在节点上设置标签,但我找不到任何关于边缘的具体内容(在plot命令中添加edge_label=...不起作用)。 Do you know how to fix the problem? 你知道如何解决这个问题吗? Thanks in advance for your help! 在此先感谢您的帮助!

using vertex_label= is equivalent to g.vs= 使用vertex_label=相当于g.vs=

so to label your edges, use g.es= : 所以要标记边缘,请使用g.es=

g.es["label"] = ["A", "B", "C"]

or 要么

g.es["name"] = map(str, np.arange(N))

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

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