Heatmap: Visualizing a Graph

This example shows how to visualize graphs using a heatmap.

import graspy

import numpy as np
%matplotlib inline
/opt/buildhome/python3.6/lib/python3.6/site-packages/sklearn/utils/deprecation.py:144: FutureWarning: The sklearn.mixture.gaussian_mixture module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.mixture. Anything that cannot be imported from sklearn.mixture is now part of the private API.
  warnings.warn(message, FutureWarning)

Plotting graphs using heatmap

Simulate graphs using weighted stochastic block models

The 2-block model is defined as below:

\begin{align*} P = \begin{bmatrix}0.8 & 0.2 \\ 0.2 & 0.8 \end{bmatrix} \end{align*}

We generate two weight SBMs where the weights are distributed from a Poisson(3) and Normal(5, 1).

from graspy.simulations import sbm

n_communities = [50, 50]
p = [[0.8, 0.2],
     [0.2, 0.8]]

wt = np.random.poisson
wtargs = dict(lam=3)
A_poisson= sbm(n_communities, p, wt=wt, wtargs=wtargs)

wt = np.random.normal
wtargs = dict(loc=5, scale=1)
A_normal = sbm(n_communities, p, wt=wt, wtargs=wtargs)

Plot the simulated weighted SBMs

from graspy.plot import heatmap

title = 'Weighted Stochastic Block Model with Poisson(3)'

fig= heatmap(A_poisson, title=title)
title = 'Weighted Stochastic Block Model with Normal(5, 1)'

fig= heatmap(A_normal, title=title)

You can also change color maps

See here for a list of colormaps

title = 'Weighted Stochastic Block Model with Poisson(3)'

fig= heatmap(A_poisson, title=title, transform=None, cmap="binary", center=None)

Data transformations

When your graphs have values that span a large range, it is often useful to transform the data in order to visualize it properly. Below, we use a real graph which is estimated from the a structural MRI scan. The data is provided by HNU1.

The data ranges from 0 to 44813, and visualizing without a transformation will emphasize the large weights. Both log and pass-to-ranks transforms provide better visualizations of the graph.

G = np.load('./data/sub-0025427_ses-1_dwi_desikan.npy')

print((np.min(G), np.max(G)))
(0.0, 44813.0)

Without transform

title = 'Without Transformation'
fig= heatmap(G, title=title, transform=None)

With log transform

title = 'Log Transform'
fig= heatmap(G, title=title, transform='log')

With pass to ranks

title = 'Pass-to-ranks (zero-boost) Transform'
fig= heatmap(G, title=title, transform='zero-boost')