MATLAB Answers

Use a different colormap scale for different sections of a matrix

4 views (last 30 days)
Sean McKee
Sean McKee on 5 Apr 2021
Commented: Adam Danz on 6 Apr 2021
I have a 15-dimensional data set and I would like to view the correlation matrix and the covariance matrix in 1 image. To do this I use the upper half triangle + the diagonal of a 15x15 matrix for the covariance data and the lower half triangle for the correlation data. I would like to apply a heatmap to this matrix, but have the covariance color mapping scale be independent of the correlation color map scale.
%Generate some data, and scale it so that covariance is distinct from
%correlation
exampleData = rand(100,15)*50;
exampleCov = cov(exampleData);
exampleCorr = corrcoef(exampleData);
%Creating and plotting the desired matrix
matrixToPlot = triu(exampleCov) + tril(exampleCorr,-1);
heatmap(matrixToPlot);
However I would like to be able to have two different scales for the colormap, one which is used for the lower correlation data and a different one for the covariance.
I considered plotting exampleCov and exampleCorr seperately, but this just overwrites the latter onto the former:
figure
heatmap(triu(exampleCov));
heatmap(tril(exampleCorr,-1));
%This just yields a plot of exampleCorr
This would be easy if I could set 0 values to NaN, and assign NaN color as 'none', but setting NaN color as 'none' is not supported.
  2 Comments

Sign in to comment.

Answers (3)

darova
darova on 6 Apr 2021
YOu can create your own color matrix
[x,y,z] = peaks(30);
ix = x.^2 + y.^2 < 2^2;
c1 = z*0;
c2 = z*0;
c1(ix) = 1;
c2(~ix) = 1;
C = cat(3,c1,c1*0,c2);
surf(x,y,z,C)
  1 Comment
Sean McKee
Sean McKee on 6 Apr 2021
Hmmm I think I see how I could finangle the color matrix to be what I want, but it appears that heat map does not accept a color matrix as input.

Sign in to comment.


Adam Danz
Adam Danz on 6 Apr 2021
Edited: Adam Danz on 6 Apr 2021
> This would be easy if I could set 0 values to NaN, and assign NaN color as 'none', but setting NaN color as 'none' is not supported.
True, but you can set NaN colors to some other color. Setting a color to none with an object that accepts this option just makes the object invisible. For a grid of colors in heatmap, you can set the missing value tiles are any color you want using the MissingDataColor property.
1. Set 0s to NaNs
matrixToPlot(matrixToPlot==0) = NaN;
Or, for floating decimals,
matrixToPlot(abs(matrixToPlot)<max(eps(matrixToPlot(:)))) = NaN;
2. Create heatmap
hm = heatmap(matrixToPlot);
3. Change NaN color
It can't be changed to none but you can change it to white or any other color.
hm.MissingDataColor = 'w';
  2 Comments
Adam Danz
Adam Danz on 6 Apr 2021
You can't plot two heatmaps superimposed.
You can only change the values of the heatmap input matrix.
If you use imagesc, then you can superimpose two axes although I wouldbn't recommend doing that.

Sign in to comment.


Sean McKee
Sean McKee on 6 Apr 2021
I'm thinking the way to do this might be to generate the desired color-mapping by rescaling the covariance values appropriately, inputing into heatmap, but then overlaying the original values as text labels, as described on the heatmap examples page:
However, I would still like to be able to display two different colorbars near the plot, and I'm not sure how to get that to work.
  1 Comment
Adam Danz
Adam Danz on 6 Apr 2021
I highly suggest you do this with imagesc instead of heatmap. Heatmap is quite difficult to customize.
Here's an answer that creates a "heatmap" using the heatmap function and using imagesc.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!