How can I set the NaN values to black in a pcolor plot?

85 Ansichten (letzte 30 Tage)
Ashfaq Ahmed
Ashfaq Ahmed am 18 Mai 2023
Bearbeitet: Ashfaq Ahmed am 18 Mai 2023
Hi all!
Is there any way to set the NaN values as black/grey or any other color instead of white?
I am using this code but it's not working.
% Generate sample data
data = rand(10, 10);
data(3, 3) = NaN; % Set a specific value as NaN
% Create the pcolor plot
figure;
pcolor(data);
shading interp;
colorbar;
% Set NaN values to black
colormap(gca, 'jet');
colormap(gca, 'parula'); % or any other colormap you prefer
c = colorbar;
c.Label.String = 'Temperature';
% Set NaN values to black
colormap(gca, 'parula');
caxis([nanmin(data(:)), nanmax(data(:))]);
cmap = colormap;
cmap(1, :) = [0 0 0]; % Set the first row of colormap to black
colormap(gca, cmap);
% Add colorbar labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('pcolor Plot with NaN Values Set to Black');
any feedback will be much appreciated!!

Akzeptierte Antwort

the cyclist
the cyclist am 18 Mai 2023
Bearbeitet: the cyclist am 18 Mai 2023
Set the axes background color to black:
% Generate sample data
data = rand(10, 10);
data(3, 3) = NaN; % Set a specific value as NaN
% Create the pcolor plot
figure;
pcolor(data);
shading interp;
colorbar;
% Set axis background color to black, so it "shows through" at the missing
% values
set(gca,'Color','black')
% Add colorbar labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('pcolor Plot with NaN Values Set to Black');

Weitere Antworten (1)

VBBV
VBBV am 18 Mai 2023
% Generate sample data
data = rand(10, 10);
data(3, 3) = NaN; % Set a specific value as NaN
% Create the pcolor plot
figure;
h = pcolor(data);
h.CData(2:4,2:4) = 0;
shading interp;
colorbar;
% Set NaN values to black
c = colorbar;
c.Label.String = 'Temperature';
%
% % Set NaN values to black
colormap(gca, 'parula');
caxis([nanmin(data(:)), nanmax(data(:))]);
cmap = colormap;
cmap(1, :) = [0 0 0]; % Set the first row of colormap to black
colormap(gca, cmap);
% Add colorbar labels and title
xlabel('X-axis');
ylabel('Y-axis');
title('pcolor Plot with NaN Values Set to Black');

Kategorien

Mehr zu Colormaps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by