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

73 visualizaciones (últimos 30 días)
Ashfaq Ahmed
Ashfaq Ahmed el 18 de Mayo de 2023
Editada: Ashfaq Ahmed el 18 de Mayo de 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!!

Respuesta aceptada

the cyclist
the cyclist el 18 de Mayo de 2023
Editada: the cyclist el 18 de Mayo de 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');

Más respuestas (1)

VBBV
VBBV el 18 de Mayo de 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');
  1 comentario
VBBV
VBBV el 18 de Mayo de 2023
One way is you can set the colordata vlaues to 0
h.CData(2:4,2:4) = 0;

Iniciar sesión para comentar.

Categorías

Más información sobre Colormaps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by