Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labelling the edges in a graph with python igraph

Tags:

python

igraph

I have the adjacency matrix (i.e. 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!

like image 449
user2983638 Avatar asked Jan 15 '14 15:01

user2983638


People also ask

How do you plot a graph in an Igraph in Python?

First, you will need to install python-igraph if you do not have it already installed. You can use pip. You will also need to install cairocffi to plot the graphs. If you are using a Python package manager such as Anaconda or Miniconda, you can install python-igraph using the conda install command.

Which is better Igraph or NetworkX?

NetworkX is pure Python, well documented and handles changes to the network gracefully. iGraph is more performant in terms of speed and ram usage but less flexible for dynamic networks. iGraph is a C library with very smart indexing and storage approaches so you can load pretty large graphs in ram.


1 Answers

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))
like image 69
punkrockpolly Avatar answered Sep 29 '22 11:09

punkrockpolly