How to plot a matrix containing NaN?

18 次查看(过去 30 天)
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 个评论
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