set NaN as another color than default using imagesc

Hello, I have a matrix filled probability numbers (i.e. ranging from 0 to 1) or NaN when the probability is not computed. I would like to display this matrix as a color table (e.g. using imagesc), in order to have a quick visualisation of the result. The colorbar range is thus set as 0 to 1 since I am interested in probability values. However, I would like the NaN fields to appear with another color than the default "-inf" (here the color of 0 since the down limit for the color is set for value 0), for example gray. How can I do this? Thank you very much, Gaelle

 採用された回答

Kelly Kearney
Kelly Kearney 2013 年 7 月 12 日
編集済み: Kelly Kearney 2013 年 7 月 12 日

11 投票

Do you need to use imagesc, or are you open to using pcolor? The latter will leave NaNs blank, so they will appear the same color as the axis background.
Example:
data = rand(10);
data(data > 0.9) = NaN;
[nr,nc] = size(data);
subplot(2,1,1);
imagesc(data);
subplot(2,1,2);
pcolor([data nan(nr,1); nan(1,nc+1)]);
shading flat;
set(gca, 'ydir', 'reverse');
(The NaN-padding in the pcolor call is so the last row and column are shown, similar to imagesc)

3 件のコメント

SeanC
SeanC 2017 年 1 月 20 日
Hi Kelly,
This solution was helpful thanks. I was wondering if there is a way I can manipulate the scale of the plot using this code - i.e. the equivalent of the clims argument in imagesc. This is because I have different plots but want them all to take the same scale.
Thanks,
Sean
Kelly Kearney
Kelly Kearney 2017 年 1 月 20 日
Just set the 'clim' property of the axes:
data = rand(10);
data(data > 0.9) = NaN;
[nr,nc] = size(data);
ax(1) = subplot(2,1,1);
imagesc(data);
ax(2) = subplot(2,1,2);
pcolor([data nan(nr,1); nan(1,nc+1)]);
shading flat;
set(gca, 'ydir', 'reverse');
set(ax, 'clim', [0 1]);
SeanC
SeanC 2017 年 1 月 20 日
Thanks!

サインインしてコメントする。

その他の回答 (5 件)

Charles Krzysik
Charles Krzysik 2017 年 4 月 27 日

40 投票

If you want to use imagesc rather than pcolor, you can set the AlphaData property to zero everywhere you have a NaN. This will show the background wherever there is no data; you can then additionally set the background colour, if you so choose. In this example I'm setting it to black:
imAlpha=ones(size(Data_Array));
imAlpha(isnan(Data_Array))=0;
imagesc(Data_Array,'AlphaData',imAlpha);
set(gca,'color',0*[1 1 1]);

4 件のコメント

JoeB
JoeB 2017 年 9 月 13 日
This is a really nice answer. Reading in netCDF files with missing values yields NaNs in MATLAB, so this allows me to make quick maps with the NaNs masked out.
teng zhang
teng zhang 2018 年 1 月 12 日
It is so nice for your answer.
TheStranger
TheStranger 2021 年 6 月 2 日
編集済み: TheStranger 2021 年 6 月 2 日
This is the most useful comment of this thread, yet not with the most votes!
Maria Cristina Araya Rodriguez
Maria Cristina Araya Rodriguez 2023 年 11 月 17 日
I agree, many thanks, in fact this is a good solution when using a ".cpt" colour scale.

サインインしてコメントする。

Lauren
Lauren 2017 年 11 月 7 日

21 投票

imagesc(data,'AlphaData',~isnan(data))

8 件のコメント

Huayan Wang
Huayan Wang 2018 年 2 月 20 日
編集済み: Huayan Wang 2018 年 2 月 20 日
I have to say this is a genius solution!
Thank you so much! This helps me a lot!
Xiaowei Zhou
Xiaowei Zhou 2019 年 1 月 11 日
Excellent idea.
Walter Roberson
Walter Roberson 2019 年 1 月 11 日
you might need double(~isnan(data)) as alphadata cannot be logical .
Shakir Hussain
Shakir Hussain 2019 年 3 月 18 日
How to use it for array data, it is giving error for array data(3d)
Error using image
Bad property value found.
Object Name: image
Property Name: 'AlphaData'.
Error in imagesc (line 20)
hh = image(varargin{:},'CDataMapping','scaled');
Walter Roberson
Walter Roberson 2019 年 3 月 18 日
please show your code calling imagesc
jichong han
jichong han 2021 年 12 月 1 日
perfect
zhou weiyan
zhou weiyan 2023 年 3 月 24 日
perfect
Walter Roberson
Walter Roberson 2023 年 11 月 17 日
If you have RGB data, some elements of which might be NaN, then
%with sufficiently new versions of MATLAB
scaled_data = rescale(YourRGBData, 0, 1);
alpha_data = 0 + ~any(isnan(YourRGBData), 3);
image(scaled_data, 'AlphaData', alpha_data);
%older versions of MATLAB
scaled_data = mat2gray(YourRGBData);
alpha_data = 0 + ~any(isnan(YourRGBData), 3);
image(scaled_data, 'AlphaData', alpha_data);
In practice if your RGB data is single or double precision data that uses only integral values 0 to 255, but also has some NaN, then you would probably use slightly different code,
scaled_data = uint8(YourRGBData);
alpha_data = 0 + ~any(isnan(YourRGBData), 3);
image(scaled_data, 'AlphaData', alpha_data);

サインインしてコメントする。

Evan
Evan 2013 年 7 月 12 日
編集済み: Evan 2013 年 7 月 12 日

0 投票

You can use the isnan function to find the indices of all NaNs, then set those elements to another value:
Example:
>>A = [4 NaN 3;NaN 2 1];
>>A(isnan(A)) = 255
A =
4 255 3
255 2 1

6 件のコメント

Gaëlle
Gaëlle 2013 年 7 月 12 日
移動済み: DGM 2023 年 3 月 25 日
Hi, Thank you for your answer, but this will not work in my case, because I am setting the limit of the colorbar (using caxis([0,1]) ). If I set, as you proposed, the NaN fields as e.g. 255, the color assigned to them will be equivalent to the color assigned to the value 1 (here the upper bound of the colorbar). I would rather code something that assign a specific color (grey) in the case of a NaN field. Is there any way to do this? Gaelle
Evan
Evan 2013 年 7 月 12 日
移動済み: DGM 2023 年 3 月 25 日
It depends. You always run the risk of setting the pixels in question to a value that is already used if you don't know anything specific about your image data.
Are you just wanting to visualize where your data returns as NaN? If so, I would do one of two things: I would plot my "good" data as grayscale, then turn the NaN pixels to a easily noticeable color like red. Alternatively, if you must have or prefer your data in color, you could plot your image then overlay a scatter plot where your elements are NaN.
Here's an example showing the first way:
A = rand(50); %random data
for i = 1:10 %turn some random points into NaNs
A(randi(50),randi(50),:) = NaN;
end
R = A(:,:,1); % turn your data into a "pseudo-gray" rgb image.
G = A(:,:,1);
B = A(:,:,1);
R(isnan(A(:,:,1))) = 1; %turn NaNs red
G(isnan(A(:,:,1))) = 0;
B(isnan(A(:,:,1))) = 0;
A(:,:,1) = R; %combine color slices into A
A(:,:,2) = G;
A(:,:,3) = B;
imagesc(A)
Here's an example for the second:
A = rand(50); %random data
for i = 1:10 %turn some random points into NaNs
A(randi(50),randi(50),:) = NaN;
end
imagesc(A)
hold on
[ii jj] = find(isnan(A));
scatter(ii,jj,'ok','MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[0 0 0],'LineWidth',2,'SizeData',100)
Gaëlle
Gaëlle 2013 年 7 月 12 日
移動済み: DGM 2023 年 3 月 25 日
Hi, No, the aim is still to plot the probability values. For more clarity: this matrix M is a square matrix, where each element (i,j) corresponds to a comparison (a probability calculus) between an element i and j, e.g. the element M(1,2) gives the probability value of the comparison between element 1 and 2 (note that in mx case M(1,2)≠M(2,1) ).
As for the NaN, they are here simply because I want to keep the size of the matrix similar across all calculation and display. They appear because some comparison do not arise. For example, if the jth element does not exist, the entire line j and row j will be set as NaN. For example, if I want to compare 4 elements, but the 2nd one does not exist yet, the matrix will look like:
if true
% code
0.3 NaN 0.4 0.1
NaN NaN NaN NaN
0.2 NaN 0.2 0.5
0.5 NaN 0.6 0.4
end
I thus want to display this matrix, but setting the NaN values as something specific (e.g. a grey value so that it resembles the color of the figure background). Is this more clear? Gaelle
Gaëlle
Gaëlle 2013 年 7 月 12 日
移動済み: DGM 2023 年 3 月 25 日
What you showed me is almost that. I just would have liked to keep the 'jet' map instead of turning every value into a grey scale. But I should manage to find a trick. Thank you, Gaelle
Gaëlle
Gaëlle 2013 年 7 月 12 日
移動済み: DGM 2023 年 3 月 25 日
But one thing which is bad using your way: the colorbar scale is not set according to the values of the probabilities. So actually, I cannot use your trick...
Adil Masood
Adil Masood 2015 年 12 月 10 日
移動済み: DGM 2023 年 3 月 25 日
Thanks you for a smart solution. There is just one correction: Instead of
scatter(ii,jj,'ok','MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[0 0 0],'LineWidth',2,'SizeData',100)
it should be
scatter(jj,ii,'ok','MarkerEdgeColor',[1 1 1],'MarkerFaceColor',[0 0 0],'LineWidth',2,'SizeData',100)
Its because imagesc() plots elements of matrix indexed as (row,column), while scatter() handles elements as (x,y).

サインインしてコメントする。

Gaëlle
Gaëlle 2013 年 7 月 12 日

0 投票

Hi, I am open to use pcolor, it just seemed more complicated to use than imagesc. I will try to implement it. Thank you!
Ben
Ben 2014 年 12 月 4 日
編集済み: Ben 2014 年 12 月 10 日

0 投票

If using pcolor, NaNs are set to the axis background color. So you can set your colormap to color non-NaNs, and set the axis background color to set the color for NaNs. For example, to set NaNs black you can use: set(gca,'color','k')

1 件のコメント

Walter Roberson
Walter Roberson 2017 年 1 月 20 日
For pcolor the nan are not exactly set to the axes background color: instead a hole is created that allows whatever is under to be visible. If nothing else is there that would be the axes background, but there could also be a different graphics object there.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeData Distribution Plots についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by