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

조회 수: 58 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2023년 5월 18일
편집: Ashfaq Ahmed 2023년 5월 18일
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!!

채택된 답변

the cyclist
the cyclist 2023년 5월 18일
편집: the cyclist 2023년 5월 18일
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');

추가 답변 (1개)

VBBV
VBBV 2023년 5월 18일
% 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
VBBV
VBBV 2023년 5월 18일
One way is you can set the colordata vlaues to 0
h.CData(2:4,2:4) = 0;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by