mac389
mac389

Reputation: 3133

Label groups in a heat map

I have an array where the ij th entry is the number of genes common to areas i and j that are differentially expressed in i with respect to j.

Labeling every xtick and ytick will make the graph too crowded. Similar to this question and this question I want to group labels on my x-axis.

The xticklabels of the heat map in the following image from Hawrylycz et al (2012) are a good example of what I want The xticklabels refer to more general regions. For example, all the columns under frontal lobe corrsepond to structures in the brain within the frontal lobe. Hawrylycz et al. (2012)

I am not trying to replicate the yticklabels, or bar graph inset.

My approach

For each box in the heat map I have an ontology. I am choosing to plot structures in a few regions, for example only the "frontal lobe and parietal lobe."

Using the ontology I can discover the start and end index of the group of columns for each structure. How do I use those indices to draw a group label?

Upvotes: 3

Views: 3247

Answers (1)

cphlewis
cphlewis

Reputation: 16259

Like so:

import pandas as pd
from numpy.random import random_integers
from numpy import reshape
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, FixedFormatter
alph = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
lalph = list(alph.lower())
alph = list(alph)

df = pd.DataFrame(random_integers(0,100,(26,26)),columns=alph,
                  index=lalph)

# Two lines just to make a plaid image in imshow 
differ = reshape([sum(df[col2]-df[col]) for col2 in df for col in df], (26,26))
differ = pd.DataFrame(differ, columns=alph,index=alph)

# pick the labels you want
ticks = [2, 14, 18, 19, 22] # C=2 because A=0 because Python is 0-indexed
ticklabels = [alph[x] for x in ticks]

fig = plt.figure(figsize=(3,5))
ax = fig.add_subplot(111)
ax.imshow(differ)
ax.autoscale(False)

# display only the chosen ticks and ticklabels
ax.xaxis.set_major_locator(FixedLocator(ticks))
ax.xaxis.set_major_formatter(FixedFormatter(ticklabels))

enter image description here

You'll have a list of strings naming genes, not a string being used as a list of letters, but the imshow axis indexes are still the indexes of the underlying numpy array.

Upvotes: 2

Related Questions