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

조회 수: 85 (최근 30일)
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);
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 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);
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 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;
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
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에서 Colormaps에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by