0

I'm starting to use python igraph in combination with networkx as the former has implementations of very recent advances in network community detection.

For now I'm simply starting with a weighted, nonsymmetric adjacency matrix and dictionary of node labels. I create a directed graph, G, in networkx, which I then converted to an igraph graph, g, and plotted the result in igraph with labelled nodes.

import numpy as npy
import networkx as nx
import igraph as ig

# Create adjacency matrix, A, and corresponding directed graph in networkx
A=npy.matrix([[4,7,7,0,0],[3,0,6,0,0],[7,6,0,2,1],[0,0,2,0,4],[0,0,1,4,0]])
G = nx.from_numpy_matrix(A, create_using=nx.DiGraph)

# Dictionary of node labels, simply 'A', 'B', ...'E' for this example
labels_dict={0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E'}
labels=list(labels_dict.values())

# Convert networkx graph to igraph
nx.write_graphml(G,'graph.graphml')
g = ig.read('graph.graphml',format="graphml")

# Plot directed graph using igraph with labeled vertices, 
ig.plot(g,vertex_label =  labels)

This creates the desired graph, but I'd like to know the most efficient means for labelling the edges with their corresponding weights.

Mayur
  • 4,345
  • 3
  • 26
  • 40
fishbacp
  • 1,123
  • 3
  • 14
  • 29

1 Answers1

0

This works:

ig.plot(g,vertex_label =  labels,vertex_label_size = 10, vertex_size = 20 
,edge_label = [edge for edge in g.es['weight']],edge_width = [.25*edge for edge 
in g.es['weight']])

Arcs can make it difficult to see which weight is associated each edge, which can be addressed by adding the option edge_curved=0.

fishbacp
  • 1,123
  • 3
  • 14
  • 29