title: Color heatmap based on grouping (or class) in python
body: <p>Can anyone help me produce a colored correlation matrix based on class so that I can see clusters. I have a nparray with the values and another nparray with that contains each element's class colour (hex color code). How can I make a heatmap that has the correlation values for each cell, but colored by the class (six different colors). Here is an example. <a href="https://i.stack.imgur.com/bvA2b.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bvA2b.png" alt="heatmap"></a></p>
  
  <p>This <a href="https://stackoverflow.com/questions/29138951/how-to-create-predefined-color-range-in-matplotlib-heat-map-from-a-pandas-datafr">How to create predefined color range in Matplotlib heat map from a Pandas Dataframe</a> seems to be relevant, however, the solution simply used a pre-defined cmap. I am able to produce a pcolor plot, however the colours don't seem to correlate to the cluster labels. I think the cmap assignment is wrong. </p>
  
  <pre><code>import matplotlib.pyplot as plt
  from pylab import *
  import numpy as np
  import pandas as pd
  
  cpool=['#23525f','#f271c4','#33cccc','#e11212','#bf7300','#f2ea30']
  correlations = pd.DataFrame.corr(whisky.iloc[:,2:14].transpose())
  correlations = np.array(correlations)  #86x86 np.array
  
  correlation_colors = []
  for i in range(len(distilleries)):
      for j in range(len(distilleries)):
          if correlations[i,j] &lt; 0.7:                    # if low correlation,
              correlation_colors.append('#f9f9f9')         # just use white. add to the empty array
          else:                                          # otherwise,
              if whisky.Group[i] == whisky.Group[j]:     # if the groups match,
                  correlation_colors.append(cpool[whisky.Group[i]]) # color them by their mutual group (class) 0-5
              else:                                      # otherwise
                  correlation_colors.append('#e4e1ff') # color them lightgray
  
  corr_color=np.array(correlation_colors).reshape(86,86)
  colmap=matplotlib.colors.ListedColormap(corr_color)
  plt.pcolor(correlations,c=corr_color,cmap=colmap)
  </code></pre>