convert distance matrix or adjacency matrix to heatmap

1,459 views
Skip to first unread message

Hossein Hadian Jazi

unread,
Mar 11, 2015, 3:27:16 PM3/11/15
to networkx...@googlegroups.com
I am really new in python and networkx.
My goal is to plot adjacency matrix and distance matrix as a heatmap. I do the following code:

adjacencyMatrix = nx.to_numpy_matrix(G)
distanceMatrix = nx.floyd_warshall_numpy(G)

fig = pyplot.figure(figsize=(5, 5))
image= pyplot.imshow(adjacencyMatrix,cmap="Greys", interpolation="none")
plt.show() 

when I plot both graphs, graphs are shown but not in a format i like. I like to show them as a heatmap.
Could you please help me how can I do that?

Athanasios Anastasiou

unread,
Mar 12, 2015, 8:48:48 AM3/12/15
to networkx...@googlegroups.com
Hello Hossein

A heat map is composed of blobs whose "height" is mapped to a color bar. These "blobs" could be interpolating intermediate values on points that the heat map doesn't have actual values for (following some model).

I am not sure that this would be the right representation for an adjacency or distance matrix.

An adjacency matrix contains, by definition, ones and zeros. So a "checker-board" diagram might be a better way to visually present it. A diagram like this could be used to quickly (and visually) identify clusters between nodes.

The distance matrix, is going to be a matrix of NxN discrete values anyway. In fact, your "problem" when trying to visualise a distance matrix is the fact that the dynamic range of the distance (the range between the min and max values of distance) might be so big that huge values dominate the matrix. This would leave you with an image showing a few really powerful spikes that would be drowning the many smaller values that you might be interested more in.

So, in this case, I would recommend the following:

1) Use the gray colormap to visualise the adjacency matrix (as you do), if you need such a visualisation
2) Use the "rainbow" colormap (this is the blue-to-red color map that could be used to represent temperature in Matplotlib) to visualise the distance matrix BUT, before sending the matrix to imshow, pass it through a log transform, something like M = log(1+ D) where D is your original distance matrix and M is your transformed matrix. For more information on this, please see: http://homepages.inf.ed.ac.uk/rbf/HIPR2/pixlog.htm

I hope this helps.

All the best
Athanasios Anastasiou

Hossein Hadian Jazi

unread,
Mar 12, 2015, 2:56:49 PM3/12/15
to networkx...@googlegroups.com
Hi Athanasios,
Thanks for your answer. It was helpful and clear it for me.
Reply all
Reply to author
Forward
0 new messages