Heatmaps in MATLAB®

How to make a heatmap in MATLAB® with a matrix. Seven examples of colored and labeled heatmaps with custom colorscales.


size = 50;
z = zeros(size, size);
for r = 1:size
    for c = 1:size
        z(r,c) = r+c;
    end
end
fig = figure;

colormap('hot');
imagesc(z);
colorbar;

fig2plotly(fig);
data = {...
  struct(...
    'z', [1, 20, 30; 20, 1, 60; 30, 60, 1], ...
    'type', 'heatmap')...
};

plotly(data);
data = {...
  struct(...
    'z', [1, 20, 30, 50, 1; 20, 1, 60, 80, 30; 30, 60, 1, -10, 20], ...
    'x', { {'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'} }, ...
    'y', { {'Morning', 'Afternoon', 'Evening'} }, ...
    'type', 'heatmap')...
};

plotly(data);
url = 'https://raw.githubusercontent.com/plotly/datasets/master/MATLAB/Z.csv';

Z = readmatrix(url);

data = {...
  struct(...
    'z', Z, ...
    'colorscale', {
        {0.0, 'rgb(165,0,38)'},
        {0.111, 'rgb(215,48,39)'},
        {0.222, 'rgb(244,109,67)'},
        {0.333, 'rgb(253,174,97)'},
        {0.444, 'rgb(254,224,144)'},
        {0.555, 'rgb(224,243,248)'},
        {0.667, 'rgb(171,217,233)'},
        {0.778, 'rgb(116,173,209)'},
        {0.889, 'rgb(69,117,180)'},
        {1.0, 'rgb(49,54,149)'} },...
    'type', 'heatmap')...
};
    
plotly(data);