Heatmap: Visualizing a Graph

This example provides how to visualize graphs using the heatmap.

import graspy

import numpy as np
%matplotlib inline

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 the data properly. Below, we use a real graph that is estimated from the a structural MRI scan. The data is provided by HNU1.

The data ranges from 0 to 44813, and visuzliating without transformation will emphsize the large weights. Both log and pass-to-ranks transform provide a better visualization 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')