Use a different colormap scale for different sections of a matrix
10 views (last 30 days)
Show older comments
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
Adam Danz
on 6 Apr 2021
The hold on command with heatmap should give you an error indicating that the use of hold on with heatmap is not supported.
Answers (3)
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)
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
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.
Sean McKee
on 6 Apr 2021
1 Comment
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.
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!