4
votes

This is an adjacency matrix for a weighted graph, such that the element ai,j is the weight of the directed edge from node i to node j.

A = [
    [0, 1,  0,  .8, 0],
    [0, 0,  .4, 0,  .3],
    [0, 0,  0,  0,  0],
    [0, 0,  .6, 0,  .7],
    [0, 0,  0,  .2, 0]]

My main aim is to generate an illustration of that graph.

I can generate a graph in networkx like this:

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
nx.draw(G)
plt.show()

But I cannot see the weights. I'm also not super happy with the image, it isn't publication ready. Does anyone have a good way to do this?

3
TypeError: Input graph is not a networkx graph typeJainil Patel
can i remove create_using=nx.DiGraph?Jainil Patel
@JainilPatel yeah sure, as long as the diagram shows the directed edgeschasmani

3 Answers

4
votes

You need to specify that you want to draw the edge labels. For that you have to call networkx.drawing.nx_pylab.draw_networkx_edge_labels.

That has an argument pos, a dictionary with nodes as keys and positions as values. It is important you use the same layout for the nodes and labels, or else they will not align!

An easy way is to let networkx handle the layout, for example with spring_layout.

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

A = [
    [0, 1,  0,  .8, 0],
    [0, 0,  .4, 0,  .3],
    [0, 0,  0,  0,  0],
    [0, 0,  .6, 0,  .7],
    [0, 0,  0,  .2, 0]]

G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
nx.draw(G, layout)
nx.draw_networkx_edge_labels(G, pos=layout)
plt.show()

Example:

Example visualization with edge labels

Note that spring_layout uses the Fruchterman-Reingold force-directed algorithm which is non-deterministic, so your graph will almost certainly not look the same. However generally it produces good looking results so that should not be a major issue.

Documentation: networkx.drawing.layout.spring_layout, which does unfortunately not mention that it is non-deterministic.

UPDATE:

To have the labels be only the weights (and not a dictionary):

labels = nx.get_edge_attributes(G, "weight")
nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels)
3
votes
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np

A = [
    [0, 1,  0,  .8, 0],
    [0, 0,  .4, 0,  .3],
    [0, 0,  0,  0,  0],
    [0, 0,  .6, 0,  .7],
    [0, 0,  0,  .2, 0]]

G = nx.from_numpy_matrix(np.matrix(A), create_using=nx.DiGraph)
layout = nx.spring_layout(G)
nx.draw(G, layout, node_size=1000, with_labels=True, font_weight='bold',    font_size=15)
labels = nx.get_edge_attributes(G,'weight')
nx.draw_networkx_edge_labels(G,pos=layout,edge_labels=labels)
plt.show()
1
votes
A = [
    [0, 1,  0,  .8, 0],
    [0, 0,  .4, 0,  .3],
    [0, 0,  0,  0,  0],
    [0, 0,  .6, 0,  .7],
    [0, 0,  0,  .2, 0]]

import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
G = nx.from_numpy_matrix(np.matrix(A))
edge_labels=nx.draw_networkx_edge_labels(G,pos=nx.spring_layout(G))
nx.draw(G)
plt.show()

draw_networkx_edge_labels

draw_networkx_edge_labels(G, pos, edge_labels=None, label_pos=0.5, font_size=10, font_color='k', font_family='sans-serif', font_weight='normal', alpha=1.0, bbox=None, ax=None, rotate=True, **kwds)[source]

Draw edge labels.

Parameters:

G (graph) – A networkx graph

pos (dictionary) – A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2.

ax (Matplotlib Axes object, optional) – Draw the graph in the specified Matplotlib axes.

alpha (float) – The text transparency (default=1.0)

edge_labels (dictionary) – Edge labels in a dictionary keyed by edge two-tupleof text labels (default=None). Only labels for the keys in the dictionary are drawn. label_pos (float) – Position of edge label along edge (0=head, 0.5=center, 1=tail)

font_size (int) – Font size for text labels (default=12)

font_color (string) – Font color string (default=’k’ black)

font_weight (string) – Font weight (default=’normal’)

font_family (string) – Font family (default=’sans-serif’)

bbox (Matplotlib bbox) – Specify text box shape and colors.

clip_on (bool) – Turn on clipping at axis boundaries (default=True)

Returns:

dict of labels keyed on the edges

Return type:

dict