Heatmap: Visualizing a Graph

This example provides how to visualize graphs using the heatmap.

[1]:
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).

[2]:
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

[3]:
from graspy.plot import heatmap

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

fig= heatmap(A_poisson, title=title)
../../_images/tutorials_plotting_heatmaps_5_0.png
[4]:
title = 'Weighted Stochastic Block Model with Normal(5, 1)'

fig= heatmap(A_normal, title=title)
../../_images/tutorials_plotting_heatmaps_6_0.png

You can also change color maps

See here for a list of colormaps

[5]:
title = 'Weighted Stochastic Block Model with Poisson(3)'

fig= heatmap(A_poisson, title=title, transform=None, cmap="binary", center=None)
../../_images/tutorials_plotting_heatmaps_8_0.png

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.

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

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

Without transform

[7]:
title = 'Without Transformation'
fig= heatmap(G, title=title, transform=None)
../../_images/tutorials_plotting_heatmaps_12_0.png

With log transform

[8]:
title = 'Log Transform'
fig= heatmap(G, title=title, transform='log')
../../_images/tutorials_plotting_heatmaps_14_0.png

With pass to ranks

[9]:
title = 'Pass-to-ranks (zero-boost) Transform'
fig= heatmap(G, title=title, transform='zero-boost')
../../_images/tutorials_plotting_heatmaps_16_0.png