Heat Map Representation of error data

26 views (last 30 days)
RAJEEV
RAJEEV on 13 Sep 2023
Commented: Mathieu NOE on 11 Dec 2023
I have an error matrix consisting of positive and negative difference between the theoritical and practical values. I want to select the cell containing lowest positive number using heat map and ignore the negative numbers. How to do that ?
P.S. : This is a part of bigger program where difference in RMSE values of training and validation datasets represents the above error matrix.

Answers (1)

Mathieu NOE
Mathieu NOE on 14 Sep 2023
hello
I could suggest these two solutions
either based on the regular heatmap function and creating a specific colormap
the limited range that includes your min positive value is coded in red (up to you to change to another color)
or second solution is based on a Fex submission (as far as I can remember, tx to his creator !)
err = readmatrix("Error Matrix.xlsx");
err_pos = err;
err_pos(err_pos<0) = NaN;
[min_val,id]= min(err_pos,[],'all','linear'); % min positive value and linear indice
[r,c] = ind2sub(size(err),id); % convert linear indice to row / col indices
%% solution #1
figure(1)
max_val = max(abs(err),[],'all');
% this colormap will span values from -max_val to +max_val
n = 2*ceil(max_val/min_val)+1; % odd number
cmap = gray(n);
% create a "red" zone between 0 and a value slightly above the min error
rz = (n-1)/2; % row index correponding to zero
cmap((rz:rz+2),1) = 1; %
cmap((rz:rz+2),2:3) = 0; %
% create the heatmap
h = heatmap(err,'GridVisible',false);
% apply the colormap
colormap(cmap)
% % apply the color limits (full range of the data)
caxis([-max_val max_val])
%% solution #2
figure(2)
matvisual(err, 'annotation')
hold on
plot(c,r,'ro','markersize',60);
hold off
% +------------------------------------------------------+
% | Matrix Visualization with MATLAB Implementation |
% | |
% | Author: Ph.D. Eng. Hristo Zhivomirov 12/02/18 |
% +------------------------------------------------------+
%
% function: matvisual(A, varargin)
%
% Input:
% A - m-by-n-by-p matrix to be visualized
% varargin - type 'annotation' in the place of varargin if one wants to
% annotate the matrix plot (x-label, y-label, etc.)
% example :
% %% generate a test matrix
% % A = eye(10);
% % A = magic(30);
% % A = ones(10);
% % A = zeros(10);
% % A = NaN(10);
% % A = Inf(10);
% % A = rand(20, 10, 3);
% A = round(10*rand(50, 50));
% % A = round(10*rand(10, 5, 5));
% % A = abs(fft(1:10));
% % A = complex(rand(5,5), rand(5,5));
% % A = char(1300*ones(10));
% % A = zeros(0, 20);
%
% %% visualize the matrix
% matvisual(A, 'annotation')
function matvisual(A, varargin)
% input validation
validateattributes(A, {'single', 'double', 'logical'}, ...
{'3d', 'nonempty', 'real'}, ...
'', 'A', 1)
% determine the matrix size
[M, N, P] = size(A);
% loop through the matrix pages
for p = 1:P
% get prepared for page-by-page visualization
if P > 1, subplot(1, P, p), end
% visualize the matrix
himg = imagesc(A(:, :, p));
grid on
% values labeling
for m = 1:M
for n = 1:N
text(n, m, num2str(A(m, n, p), 3), ...
'FontName', 'Times New Roman', ...
'FontSize', round(6 + 50./sqrt(M.*N)), ...
'HorizontalAlignment', 'center', ...
'Rotation', 45)
end
end
% annotation
if strcmp(varargin, 'annotation')
% x-label, y-label, x-ticks, y-ticks, title
set(gca, 'FontName', 'Times New Roman', 'FontSize', 12)
xlabel('Column number')
ylabel('Row number')
if P > 1, title(['Matrix page ' num2str(p)]), end
if M <= 50, set(gca, 'YTick', 1:M), end
if N <= 50, set(gca, 'XTick', 1:N), end
end
% set the datatip UpdateFcn
cursorMode = datacursormode(gcf);
set(cursorMode, 'UpdateFcn', {@datatiptxt, himg})
end
end
function text_to_display = datatiptxt(~, hDatatip, himg)
% determine the current datatip position
pos = get(hDatatip, 'Position');
% form the datatip label
text_to_display = {['Row: ' num2str(pos(2))], ...
['Column: ' num2str(pos(1))], ...
['Value: ' num2str(himg.CData(pos(2), pos(1)))]};
end
  1 Comment
Mathieu NOE
Mathieu NOE on 11 Dec 2023
hello again
do you mind accepting my answer (if it has fullfiled your expectations ) ? tx

Sign in to comment.

Categories

Find more on Geographic Plots in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!