How to plot a matrix containing NaN?

23 ビュー (過去 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')
  1 件のコメント
Abhishek Chakraborty
Abhishek Chakraborty 2021 年 9 月 12 日
Thanks a lot!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by