Filter löschen
Filter löschen

How to plot a matrix containing NaN?

10 Ansichten (letzte 30 Tage)
Abhishek Chakraborty
Abhishek Chakraborty am 12 Sep. 2021
Kommentiert: Dave B am 27 Sep. 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?

Akzeptierte Antwort

Dave B
Dave B am 12 Sep. 2021
Bearbeitet: Dave B am 12 Sep. 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 Kommentare
Abhishek Chakraborty
Abhishek Chakraborty am 27 Sep. 2021
Yes.
Dave B
Dave B am 27 Sep. 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')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 12 Sep. 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')

Kategorien

Mehr zu Orange finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by