set NaN as another color than default using imagesc
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (5)
Charles Krzysik
el 27 de Abr. de 2017
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 comentarios
JoeB
el 13 de Sept. de 2017
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
el 12 de En. de 2018
It is so nice for your answer.
TheStranger
el 2 de Jun. de 2021
Editada: TheStranger
el 2 de Jun. de 2021
This is the most useful comment of this thread, yet not with the most votes!
Maria Cristina Araya Rodriguez
el 17 de Nov. de 2023
I agree, many thanks, in fact this is a good solution when using a ".cpt" colour scale.
Lauren
el 7 de Nov. de 2017
21 votos
imagesc(data,'AlphaData',~isnan(data))
8 comentarios
Huayan Wang
el 20 de Feb. de 2018
Editada: Huayan Wang
el 20 de Feb. de 2018
I have to say this is a genius solution!
Thank you so much! This helps me a lot!
Xiaowei Zhou
el 11 de En. de 2019
Excellent idea.
Walter Roberson
el 11 de En. de 2019
you might need double(~isnan(data)) as alphadata cannot be logical .
Shakir Hussain
el 18 de Mzo. de 2019
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
el 18 de Mzo. de 2019
please show your code calling imagesc
jichong han
el 1 de Dic. de 2021
perfect
zhou weiyan
el 24 de Mzo. de 2023
perfect
Walter Roberson
el 17 de Nov. de 2023
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);
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 comentarios
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)
Adil Masood
el 10 de Dic. de 2015
Movida: DGM
el 25 de Mzo. de 2023
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
el 12 de Jul. de 2013
0 votos
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 comentario
Walter Roberson
el 20 de En. de 2017
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.
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!