How to plot a matrix containing NaN?

17 visualizaciones (últimos 30 días)
Abhishek Chakraborty
Abhishek Chakraborty el 12 de Sept. de 2021
Comentada: Dave B el 27 de Sept. de 2021
I have a MATLAB 2-D array which I want to plot which contain only 2 numbers : 0 and 1. It also contains a few NaNs. I want to plot it. The 2-D array is like this:
A=[1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
0 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 0 NaN
1 1 1 NaN
1 1 1 1
1 1 1 1
1 1 1 NaN
1 1 1 NaN
1 1 1 1
1 0 1 NaN
1 1 1 1];
I used the following code trying to plot it:
pcolor(1:4,1:29,A);
colorbar;
But it is not showing NaNs. Also, it is only showing results till the first 3 columns of the 2-D array. Can someone help me with it?

Respuesta aceptada

Dave B
Dave B el 12 de Sept. de 2021
Editada: Dave B el 12 de Sept. de 2021
pcolor specifies color at the vertex, which (confusingly) means that you have one less row and column. It does great with NaN though, marking them as white regardless of the colormap. You can pad your array and see everything, NaN's won't be indicated in the colorbar:
A = getA; % putting it in a function so it's not at the top of the answer
pcolor(padarray(A,[1 1],'post'))
colorbar
Alternatively you can use image (or imagesc) but you might need to do something special with your colormap to differentiate NaN and 0:
imagesc(A)
colormap([1 1 1;lines(2)]);
caxis([-1 1]); % NaN indicated 'below' the bottom value
c=colorbar;
c.Ticks=[-2 0 2]./3;
c.TickLabels={'NaN' '0' '1'};
xticks(1:4); % note that image's ticks are aligned to the faces, pcolor's are aligned to the vertices
Note that imagesc and pcolor do something different with the direction of the y axis. you can use set(gca,'YDir', ...) to set it how you like it or axis xy/axis ij
function A=getA
A=[1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
0 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 0 NaN
1 1 1 NaN
1 1 1 1
1 1 1 1
1 1 1 NaN
1 1 1 NaN
1 1 1 1
1 0 1 NaN
1 1 1 1];
end
  4 comentarios
Abhishek Chakraborty
Abhishek Chakraborty el 27 de Sept. de 2021
Yes.
Dave B
Dave B el 27 de Sept. de 2021
is the goal here to produce just one dot for each location where Z<.05? I don't know stipple well, is it producing many dots for each square or just one?
Could you do something like (one dot/square case): scatter(X(mask(:)),y(mask(:)),'k','filled')

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 12 de Sept. de 2021
Colors were chosen arbitrarily. Black is the 0's, orange is the 1's, yellow is the nan.
A=[1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
0 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 1 NaN
1 1 0 NaN
1 1 1 NaN
1 1 1 1
1 1 1 1
1 1 1 NaN
1 1 1 NaN
1 1 1 1
1 0 1 NaN
1 1 1 1];
alpha = double(~isnan(A));
imagesc(A, 'AlphaData', alpha);
colormap([0 0 0; .9 .5 .3])
set(gca, 'color', 'y')

Categorías

Más información sobre Orange en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by