Heatmaps

[1]:
import mgkit.plots
import numpy
import pandas
import seaborn as sns
import matplotlib.colors

Random matrix and color map init

[2]:
nrow = 50
ncol = nrow

data = pandas.DataFrame(
{
    x: numpy.random.negative_binomial(500, 0.5, nrow)
    for x in xrange(ncol)
}
)

[3]:
sns.palplot(sns.color_palette('Blues', 9))
../../_images/examples_plots_heatmap_4_0.png
[4]:
cmap = matplotlib.colors.ListedColormap(sns.color_palette('Blues', 9))

Basic plot

[5]:
fig, ax = mgkit.plots.get_single_figure(figsize=(10,10), aspect='equal')
mgkit.plots.heatmap.baseheatmap(data, ax, cmap=cmap)
[5]:
<matplotlib.collections.QuadMesh at 0x7f83af51e850>
../../_images/examples_plots_heatmap_7_1.png

Add numbers to the heatmap

Default

[6]:
fig, ax = mgkit.plots.get_single_figure(figsize=(20,20))
mgkit.plots.heatmap.baseheatmap(data.iloc[:20, :20], ax, cmap=cmap, annot=True)
[6]:
<matplotlib.collections.QuadMesh at 0x7f83ab9d1110>
../../_images/examples_plots_heatmap_10_1.png

Change format of numbers

[7]:
fig, ax = mgkit.plots.get_single_figure(figsize=(10,10))
mgkit.plots.heatmap.baseheatmap(
    data.iloc[:10, :10],
    ax,
    cmap=cmap,
    annot=True,
    annotopts=dict(format=lambda x: "{:.1f}".format(x))
)
[7]:
<matplotlib.collections.QuadMesh at 0x7f83af5636d0>
../../_images/examples_plots_heatmap_12_1.png
[8]:
fig, ax = mgkit.plots.get_single_figure(figsize=(15,15))
mgkit.plots.heatmap.baseheatmap(
    data.iloc[:20, :20],
    ax,
    cmap=cmap,
    annot=True,
    annotopts=dict(
        format=lambda x: "%.1f" % x,
        fontsize=10,
        color='r'
    )
)
[8]:
<matplotlib.collections.QuadMesh at 0x7f83ad6f5310>
../../_images/examples_plots_heatmap_13_1.png

Using Boundaries for the colors

[9]:
norm = matplotlib.colors.BoundaryNorm([0, 300, 500, 700, 900, 1000], cmap.N)

fig, ax = mgkit.plots.get_single_figure(figsize=(10,10), aspect='equal')
mgkit.plots.heatmap.baseheatmap(data, ax, cmap=cmap, norm=norm)
[9]:
<matplotlib.collections.QuadMesh at 0x7f83ad3a6a10>
../../_images/examples_plots_heatmap_15_1.png

Normalising the colors

[10]:
norm = matplotlib.colors.Normalize(vmin=400, vmax=700, clip=True)

fig, ax = mgkit.plots.get_single_figure(figsize=(10,10), aspect='equal')
mgkit.plots.heatmap.baseheatmap(data, ax, cmap=cmap, norm=norm)
[10]:
<matplotlib.collections.QuadMesh at 0x7f83ad1fe710>
../../_images/examples_plots_heatmap_17_1.png

Grouping labels

[11]:
fig, ax = mgkit.plots.get_single_figure(figsize=(10,10), aspect='equal')
mgkit.plots.heatmap.baseheatmap(data, ax, cmap=cmap)
mgkit.plots.grouped_spine(
    [range(10), range(10, 20), range(20, 30), range(30, 40), range(40, 50)],
    ['first', 'second', 'third', 'fourth', 'fifth'],
    ax
)
../../_images/examples_plots_heatmap_19_0.png

Reversing the order of the rows

[12]:
fig, ax = mgkit.plots.get_single_figure(figsize=(10,10), aspect='equal')
mgkit.plots.heatmap.baseheatmap(data.loc[data.index[::-1]], ax, cmap=cmap)
mgkit.plots.grouped_spine(
    [range(10), range(10, 20), range(20, 30), range(30, 40), range(40, 50)][::-1],
    ['first', 'second', 'third', 'fourth', 'fifth'][::-1],
    ax
)
../../_images/examples_plots_heatmap_21_0.png

A dendrogram from clustering the data

Clustering rows

[13]:
fig, ax = mgkit.plots.get_single_figure(figsize=(20, 5))
_ = mgkit.plots.heatmap.dendrogram(data, ax)
/mnt/c/Users/frubino/Documents/repositories/mgkit/mgkit/plots/heatmap.py:241: ClusterWarning: scipy.cluster: The symmetric non-negative hollow observation matrix looks suspiciously like an uncondensed distance matrix
  clusters = hclust.linkage(pairwise_dists, method=method)
../../_images/examples_plots_heatmap_24_1.png

Clustering colums (You need the transposed matrix)

[14]:
fig, ax = mgkit.plots.get_single_figure(figsize=(20, 5))
_ = mgkit.plots.heatmap.dendrogram(data.T, ax)
../../_images/examples_plots_heatmap_26_0.png

A simple clustered heatmap, look at the code for customisation

[15]:
mgkit.plots.heatmap.heatmap_clustered(data, figsize=(20, 15), cmap=cmap)
../../_images/examples_plots_heatmap_28_0.png