Use a different colormap scale for different sections of a matrix
조회 수: 14 (최근 30일)
이전 댓글 표시
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
Adam Danz
2021년 4월 6일
The hold on command with heatmap should give you an error indicating that the use of hold on with heatmap is not supported.
답변 (3개)
darova
2021년 4월 6일
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
2021년 4월 6일
편집: Adam Danz
2021년 4월 6일
> 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
Adam Danz
2021년 4월 6일
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
2021년 4월 6일
댓글 수: 1
Adam Danz
2021년 4월 6일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!