Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple heatmaps in matplotlib

I am currently facing the problem of visualizing three dimensional data. Concretely, I have two parameters that are varying and the third dimension is the resulting output which in this case is a value between zero and one (percentage).

I have several distinct datasets that I want to illustrate. It is working well with using heatmaps in matplotlib (pcolor).

However, I want to directly compare the distinct datasets with each other. I am not quite happy with producing a seperate plot for each dataset and representing it this way. I somehow want to plot it in one figure to be able to directly compare them.

I have tried 3D plots (scatter and surface) which is working quite decent, but the values are overlapping and most of the time you can only see one dataset. 3D Plots are really not working that well.

So my main question is if someone has an idea of how I could represent this in one plot.

Regards!

like image 808
fsociety Avatar asked Jul 23 '13 11:07

fsociety


People also ask

How do you combine heatmaps?

To concatenate heatmaps, simply use + operator. Under default mode, dendrograms from the second heatmap will be removed and row orders will be the same as the first one. Also row names for the first two heatmaps are removed as well. The returned value of the concatenation is a HeatmapList object.


2 Answers

Although it 's an old question, i recently did something related: plotting two heatmaps in the same figure. I did that by converting the squares into scatterplots where i converted the squares to two triangles.

I made the two triangles by using custom markers:

 import matplotlib
 import matplotlib.pyplot as plt
 import numpy as np

 def getCustomSymbol1(path_index=1):
  if path_index==1:  #upper triangle
      verts = [
      (0.0,0.0),
      (1.0,0.0),
      (1.0,1.0),
      (0.0,0.0),]
  else:              #lower triangle
      verts = [
      (0.0,0.0),
      (0.0,1.0),
      (1.0,1.0),
      (0.0,0.0),]
  codes = [matplotlib.path.Path.MOVETO,
           matplotlib.path.Path.LINETO,
           matplotlib.path.Path.LINETO,
           matplotlib.path.Path.CLOSEPOLY,
           ] 
  pathCS1 = matplotlib.path.Path(verts, codes)
  return pathCS1, verts

 def plot_mat(matrix=np.random.rand(20,20), path_index=1, alpha=1.0, vmin=0., vmax=1.):
    nx,ny = matrix.shape
    X,Y,values = zip(*[ (i,j,matrix[i,j]) for i in range(nx) for j in range(ny) ] )
    marker,verts = getCustomSymbol1(path_index=path_index)
    ax.scatter(X,Y,s=4000, 
               marker=marker, 
               c=values, 
               cmap='viridis', 
               alpha=alpha, 
               vmin=vmin, vmax=vmax )
    return

 fig = plt.figure()
 ax = fig.add_subplot(111)
 A  = np.random.uniform(20,50,30).reshape([6,5])
 B  = np.random.uniform(40,70,30).reshape([6,5])
 vmin = np.min([A,B])
 vmax = np.max([A,B])
 plot_mat(path_index=1,vmin=vmin,vmax=vmax,matrix=A)
 plot_mat(path_index=2,vmin=vmin,vmax=vmax,matrix=B)
 plt.xlim([0,6])
 plt.ylim([0,5])
 # for the colorbar i did the trick to make first a fake mappable:
 sm = plt.cm.ScalarMappable(cmap='viridis', norm=plt.Normalize(vmin=vmin, vmax=vmax ) )
 sm._A=[]
 plt.colorbar(sm)
 plt.show()

That together can give you something like: enter image description here

like image 99
JLT Avatar answered Sep 17 '22 01:09

JLT


There are a few options to present 2 datasets together:

Options 1 - draw a heatmap of the difference of 2 datasets (or ratio, whatever is more appropriate in your case)

pcolor(D2-D1)

and then present several of these comparison figures.

Option 2 - present 1 dataset as pcolor, and another as countour:

pcolor(D1)
contour(D2)

If you really need to show N>2 datasets together, I would go with contour or contourf:

contourf(D1,cmap='Blues')
contourf(D2,cmap='Reds', alpha=0.66)
contourf(D2,cmap='Reds', alpha=0.33)

example output of 3 contourf commands

or

contour(D1,cmap='Blues')
contour(D2,cmap='Reds')
contour(D2,cmap='Reds')

example output of 3 contour commands

unfortunately, simiar alpha tricks do not work well with pcolor.

like image 43
Ofri Raviv Avatar answered Sep 21 '22 01:09

Ofri Raviv