简体   繁体   中英

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:

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). Do you know how to fix the problem? Thanks in advance for your help!

using vertex_label= is equivalent to g.vs=

so to label your edges, use g.es= :

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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