set NaN as another color than default using imagesc

316 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Kelly Kearney
Kelly Kearney il 12 Lug 2013
Modificato: Kelly Kearney il 12 Lug 2013
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 Commenti
Kelly Kearney
Kelly Kearney il 20 Gen 2017
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]);

Accedi per commentare.

Più risposte (5)

Charles Krzysik
Charles Krzysik il 27 Apr 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 Commenti
TheStranger
TheStranger il 2 Giu 2021
Modificato: TheStranger il 2 Giu 2021
This is the most useful comment of this thread, yet not with the most votes!
Maria Cristina Araya Rodriguez
I agree, many thanks, in fact this is a good solution when using a ".cpt" colour scale.

Accedi per commentare.


Lauren
Lauren il 7 Nov 2017
imagesc(data,'AlphaData',~isnan(data))
  8 Commenti
Walter Roberson
Walter Roberson il 17 Nov 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);

Accedi per commentare.


Evan
Evan il 12 Lug 2013
Modificato: Evan il 12 Lug 2013
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 Commenti
Gaëlle
Gaëlle il 12 Lug 2013
Spostato: DGM il 25 Mar 2023
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 il 10 Dic 2015
Spostato: DGM il 25 Mar 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).

Accedi per commentare.


Gaëlle
Gaëlle il 12 Lug 2013
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 il 4 Dic 2014
Modificato: Ben il 10 Dic 2014
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 Commento
Walter Roberson
Walter Roberson il 20 Gen 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.

Accedi per commentare.

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by