How can I set the NaN values to black in a pcolor plot?
58 views (last 30 days)
Show older comments
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!!
0 Comments
Accepted Answer
the cyclist
on 18 May 2023
Edited: the cyclist
on 18 May 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');
0 Comments
More Answers (1)
VBBV
on 18 May 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 Comment
VBBV
on 18 May 2023
One way is you can set the colordata vlaues to 0
h.CData(2:4,2:4) = 0;
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!