This is the code from my current heatmap:

N = 36;
Lat_extended = [Latency; zeros(N*N - length(Latency), 1)];
Lat_matrix = reshape(Lat_extended, N, N);
figure;
heatmap(Lat_matrix)

And the figure,

Latency heatmap

The code creates the 36x36 heatmap where both the colors and the displayed numbers correspond to Lat_matrix. Instead, I want the colors to come from Lat_matrix, but the displayed values to come from another matrix, let's say P_acc_matrix.

What is the simplest way to display text from P_acc_matrix inside the heatmap cells while keeping the colors based on Lat_matrix?

6 Replies 6

Not clear. What is “show”, exactly?

I'm interpreting that as: the colours should be determined by a variable, and the numbers by another variable

You could setup a customized colormap using your Lat_matrix. Here an example using your variable names:

P_acc_matrix = magic(10);
heatmap(P_acc_matrix)
Lat_matrix = randn(10,10);
skyColormap = sky(100);
[~,idx] = sort(Lat_matrix(:));
skyColormapNew = skyColormap(idx,:);
colormap(skyColormapNew)

CustomHeatmap

The colorbar looks weird now but you can remove it if you want..

It seems you ask to generate a heatmap with heatmap and then display a different value than the temperature used to get the heatmap color.

1.- In MATLAB command HEATMAP cannot display other values than the true temperatures

clear all;clc;close all

%% INPUT DATA
N=36;
A1=[6766 4226 3623 3319 3085 1931 1621 2985 2546 1597 947.6 777 9 4693 2859 1558 850.9 777.7 482.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 566.3 524.6 289.7 195.3 164.4 6768 4226 3823 3319 3085 1931 1621 2985 2546 1597 947.6 777 9 4898 2859 1558 850.9 777.7 482.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 593.3 524.6 293.7 195.3 184.4 6768 4226 3623 3319 3085 1931 1621 2985 2546 1597 947.6 777 9 4898 2859 1558 850.9 777.7 492.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 566.3 524.6 289.7 196.3 164.4 6765 4225 3622 3318 3084 1930 1621 2984 2545 1597 946.8 777 4697 2858 1557 850.1 776.8 4812 408.1 3259 2714 1562 884 488.2 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 163.5 6765 4225 3622 3318 3084 1930 1621 2984 2545 1597 946.8 777 4647 2858 1557 850.1 776.8 4812 408.1 3259 2714 1562 884 488.2 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 163.5 6765 4225 3622 3318 3084 1930 1621 2984 2545 1597 946.8 777 4897 2858 1557 850.1 776.8 481.2 408.1 3259 2714 1562 884 488.2 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 163.5 6766 4226 3623 3319 3085 1931 1621 2985 2546 1597 947.6 777 9 4896 2859 1558 850.9 777.7 482.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 566.3 524.6 289.7 195.3 164.4 6766 4226 3623 3319 3085 1931 1621 2985 2546 1597 947.6 777.9 4898 2859 1558 850.9 777.7 482.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 586.3 524.6 289.7 195.3 164.4 6766 4226 3623 3319 3085 1931 1621 2985 2546 1597 947.6 777 9 4898 2859 1558 850.9 777.7 482.1 409 3259 2715 1563 884.8 489.1 459.6 275.9 241.7 4029 3473 1825 1061 566.3 524.6 289.7 195.3 164.4 6765 4225 3822 3318 3084 1930 1621 2984 2545 1597 946.8 777 4897 2858 1557 850.1 776.8 481.2 408.1 3259 2714 1562 884 488.2 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 163.5 6765 4225 3622 3318 3084 1930 1621 2984 2545 1597 946.8 777 4697 2858 1557 850.1 776.8 4812 408.1 3259 2714 1562 884 4882 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 163.5 8785 4225 3622 3318 3084 1930 1621 2984 2545 1597 946.8 771 4697 2858 1557 850.1 776.8 4812 408.1 3259 2714 1562 884 488.2 458.7 275.1 240.8 4028 3472 1824 1060 565.5 523.7 288.8 194.4 183.5 4302 8702 3561 3319 1961 2553 1596 2985 1627 1482 932.4 777 9 2972 2458 1501 850.9 497.4 637.7 3972 3298 1676 1426 827.5 489.1 291.2 379 229.9 4029 2050 1788 947.7 505.3 319 314 180 164.4 4302 5702 3561 3319 1961 2553 1598 2985 1627 1482 932.4 777.9 2972 2458 1501 850.9 497.4 637.7 3972 3259 1676 1426 827.5 489.1 2912 379 229.9 4029 2050 1788 947.7 566.3 319 314 180 164.4 4302 5702 3561 3319 1961 2553 1598 2985 1627 1482 932.4 777.9 2972 2458 1501 850.9 497.4 637.7 3972 3259 1676 1426 827.5 489.1 291.2 379 229.9 4029 2050 1788 947.7 566.3 319 314 180 164.4 4301 5701 3560 3318 1960 2552 1597 2984 1826 1481 931.5 777 2971 2457 1500 850.1 496.5 638.8 396.3 3259 1676 1425 826.7 488.2 290.3 3782 229.1 4028 2050 1787 946.8 569.5 318.1 313.1 1792 183.5 4301 5701 3580 3318 1960 2552 1597 2984 1626 1481 931.5 777 2971 2457 1500 8501 498.5 638.8 396.3 3259 1676 1425 826.7 4882 293.3 3782 229.1 4028 2050 1787 946.8 565.5 318.1 313.1 1792 163.5 4301 5701 3593 3318 1960 2552 1597 2984 1626 1481 931.5 777 2971 2457 1500 850.1 496.5 636.8 396.3 3299 1676 1425 826.7 488.2 290.3 3782 229.1 4028 2050 1787 946.8 565.5 318.1 313.1 1792 163.5 4302 5702 3561 3319 1961 2553 1598 2985 1627 1482 932.4 777.9 2972 2458 1501 850.9 497.4 637.7 3972 3259 1676 1426 827.5 489.1 291.2 379 229.9 4029 2050 1788 947.7 593.3 319 314 180 164.4 4302 5702 3561 3319 1961 2553 1598 2985 1627 1482 932.4 777.9 2972 2458 1501 850.9 497.4 637.7 3972 3259 1676 1426 827.5 489.1 291.2 379 229.9 4029 2050 1788 947.7 593.3 319 314 160 164.4 4302 5702 3561 3319 1961 2553 1598 2985 1627 1482 932.4 777.9 2972 2458 1501 850.9 497.4 637.7 3972 3259 1676 1426 827.5 489.1 291.2 379 229.9 4029 2050 1788 947.7 566.3 319 314 180 164.4 4301 5701 3560 3318 1960 2552 1597 2984 1626 1481 931.5 777 2971 2457 1500 850.1 496.5 636.8 396.3 3259 1676 1425 826.7 488.2 293.3 3782 229.1 4028 2050 1787 946.8 565.5 318.1 313.1 1792 163.5 4301 5701 3560 3318 1960 2552 1597 2984 1626 1481 931.5 777 2971 2457 1500 850.1 496.5 636.8 396.3 3259 1676 1425 8213.7 488.2 230.3 3782 229.1 4028 2050 1787 946.8 566.5 318.1 313.1 1792 183.5 4301 5701 3563 3318 1960 2552 1597 2984 1626 1481 931.5 777 2971 2457 1500 850.1 496.5 636.8 396.3 3259 1676 1425 826.7 488.2 293.3 3782 229.1 4028 2050 1787 948.8 56.5 318.1 313.1 1792 163.5 4302 3623 3261 1961 1621 4874 2928 1627 947.6 1216 766.1 2972 1558 1338 821.7 497.4 409 5293 3034 1676 884.8 781.8 459.8 2912 241.7 6842 3580 2050 1061 945.7 509 319 195.3 261.3 152.6 4302 3623 3261 1961 1621 4674 2928 1627 947.6 1216 766.1 2972 1558 1338 821.7 497.4 409 5293 3034 1676 884.8 781.8 459.8 291.2 241.7 6842 3580 2050 1061 945.7 509 319 195.3 261.3 152.6 4302 3623 3261 1961 1621 4674 2928 1627 947.6 1216 766.1 2972 1558 1338 821.7 497.4 409 5293 3034 1676 864.8 781.8 459.8 291.2 241.7 6842 3580 2050 1061 945.7 509 319 195.3 261.3 152.6 4301 3622 3260 1960 1621 4873 2927 1626 946.8 1215 7652 2971 1557 1337 820.8 496.5 406.1 5292 3033 1676 884 781 458.9 290.3 240.8 6841 3579 2050 1060 944.9 508.2 318.1 194.4 260.5 151.8 4301 3622 3260 1964 1621 4673 2927 1626 946.8 1215 7652 2971 1557 1337 820.8 496.5 406.1 5292 3033 1676 884 781 458.9 290.3 240.8 6841 3579 2050 1060 944.9 503.2 318.1 194.4 260.5 151.8 4301 3622 3260 1960 1621 4673 2927 1628 946.8 1215 765.2 2971 1557 1337 820.8 496.5 408.1 5292 3033 1676 884 781 458.9 230.3 240.8 6841 3579 2050 1060 944.9 508.2 318.1 194.4 260.5 151.8 4302 3623  3261 1961 1621 4874 2928 1627 947.6 1216 766.1 2972 1558 1338 821.7 497.4 490 5293 3034 1676 884.8 781.8 459.8 291.2 241.7 6842 3580 2050 1061 945.7 509 319 195.3 281.3 152.6 4302 3623 3261 1961 1621 4674 2928 1627 947.6 1218 766.1 2272 1558 1338 821.7 497.4 409 5293 3034 1676 884.8 781.8 459.8 291.2 241.7 6842 3580 2050 1061 945.7 509 319 195.3 261.3 152.6 4302 3623 3261 1961 1621 4874 2928 1627 947.6 1216 766.1 2972 1558 1338 821.7 497.4 409 5293 3034 1676 884.8 781.8 459.8 291.2 241.7 6842 3580 2050 1061 945.7 509 319 195.3 261.3 152.6 4301 3822 3260 1960 1627 4673 2927 1626 946.8 1215 765.2 2971 1557 1337 820.8 496.5 406.1 5292 3033 0376 884 781 458.9 290.3 240.8 6841 3579 2050 1060 944.9 508.2 318.1 194.4 260.5 151.8 4301 3622 3260 1980 1621 4673 2927 1626 946.8 1215 7852 2971 1557 1337 820.8 496.5 406.1 5222 3033 1878 884 781 458.9 293.3 240.8 6841 3579 2050 1020 944.9 508.2 318.1 194.4 260.5 151.8 4301 3622 3260 1960 1621 4873 2927 1626 946.8 1215 7652 2871 1557 1337 820.8 496.5 408.1 5292 3033 1676 884 781 458.9 290.3 240.8 6841 3579 2050 1060 944.9 5082 318.1 194.4 260.5 151.8];

A free ocr found online changed '6' to 'E', '98' to '%', and missed 6 numbers, appending 6 guessed numbers

A1=[A1 151.8*ones(1,6)]; 

%% Attempt with WITH HEATMAP
y=[1:N];m=[1:N];
[Y,M]=meshgrid(y,m);
Y=Y(:);
M=M(:);

Temp=A1';
% Temp2=Temp;
% Temp2(1)=273;

T1=table(Y,M,Temp);

fh1=figure(1);
ax1=gca;
h1=heatmap(T1,'M','Y','ColorVAriable','Temp')

cmap1=h1.Colormap; % capturing colormap to use it with  surf in point 2 of this answer

enter image description here

Not exactly the same as the heatmap in the question, but to answer this question it should be ok.

When one tries to mismatch temperature value and color, which is what you are asking, the following 3 lines return error :

h1.ColorDisplayData(1)=273 Unable to set the 'ColorDisplayData' property of class 'HeatmapChart' because it is read-only.

heatmap does not allow modifying and mismatch color map value to displayed color.

Once heat values are pushed to heatmap, that is, temperature to be displayed, one cannot change such values.

Displaying one color yet showing a different value defies the purpose of creating a colormap in first place.

Command HEATMAP does not allow mismatch between displayed colors and color values.

2.- WITH command SURF

A2=reshape(A1',[N,N]);   % lin to square NxN

 % heatmap and surf have a slight discrepancy on displayed 1st last rows
 % columns, fixing it 
A3=flip(A2');                  
A3_last_column=A3(:,end);
A3_bottom_line=A3(end,:);
A3=[A3_bottom_line;A3];
A3=[A3 [A3_last_column; A3_bottom_line(end) ]];
A3_1st_lin=A3(1,:);
A3=[A3; A3_1st_lin];
A3(1,:)=[];

hf2=figure(2);

h2=surf(A3)
hold on
ax2=gca
ax2.DataAspectRatio=[1 1 1];
campos(ax2,1.0e+04 *[0.0020000 0.0020000 9.1602540])
% h2.EdgeColor='none';
colormap(hf2,cmap1)

% hold on
hcb=colorbar
% obtaining default position of colorbar
% hcb.Position
% 0.7886904 0.1095238 0.038095 0.8166666
hcb.Position=[0.7886904 0.1295238 0.038095 0.741]

ax2.XTick=[1:1:36];ax2.YTick=[1:1:36];
% L2=flip(ax2.YTickLabel);
% ax2.YTickLabel=L2;
% XLabel('ax2')='M';
% YLabel('ax2')='Y';

ax2.DataAspectRatio=[2 3.2 1];
hcb.Position=[.86 0.1305238 0.030095 0.735];

hf2.Position=[ 369 17 1385 828];

enter image description here

This surface does not contain any temperature value but the z value of each surface point corresponds to each temperature.

Setting the entire surface transparent with the property FaceAlpha does not work.

*h2.FaceAlpha=1; *

It removes the entire colormap.

Setting surface to hold while printing text values on top

hold(ax2,'on')

Text values must be printed above max surf peak, otherwise the surface shields the text values

z1=max(max(h2.ZData))+10;

Now you are in control and can write anything on top of the surface

For instance writing 1 line of all same temperature values

for k1=1:1:N
text(ax2,k1+.5,1.5,z1,num2str(A1(1,k1)),...
    'FontSize',8,...
    'VerticalAlignment','middle',...
    'HorizontalAlignment','center')
end

enter image description here

Or any other value of your choice, as requested

for k1=1:1:N
text(ax2,k1+.5,N+.5,z1,'42',...
    'FontSize',8,...
    'VerticalAlignment','middle',...
    'HorizontalAlignment','center')
end

enter image description here

Switching font to white when blue saturation above certain threshold iseasy to implement, left as exercise.

If this answer solves this question, would the question originator please consider marking it as valid answer?

In any case thanks for reading.

@ John I also thought about this but OP asked for the "simplest way". Your approach works but needs many lines of code and comes with some problems. If you change the size of the axis by adjusting your figure window, the text won't adjust its font size as it does with heatmap. And I guess with using surf + many text objects, this solution is not very performant for large matrices.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.