How to plot a matrix containing NaN?

조회 수: 26 (최근 30일)
Abhishek Chakraborty
Abhishek Chakraborty 2021년 9월 12일
댓글: Dave B 2021년 9월 27일
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?

채택된 답변

Dave B
Dave B 2021년 9월 12일
편집: Dave B 2021년 9월 12일
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
Abhishek Chakraborty
Abhishek Chakraborty 2021년 9월 27일
Yes.
Dave B
Dave B 2021년 9월 27일
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')

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 9월 12일
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')

카테고리

Help CenterFile Exchange에서 Orange에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by