Plot function behave differently when only using line versus only using markers when plotting data with NaN values
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
I am currently trying to plot a matrix,let's call it 'A' containing NaNs in it, when I use plot(x,A, '-k'), it displays correctly (right image below), with gaps when the data is NaN. However, when I try to plot with just markers plot(x,A,'kx') (left image below), there are data points where there should be gaps (where NaN values are), there seems to be some interpolation going on.
I wonder if this is an issue with the Matlab version I have access on: R2021a - if so, is there any work around this? I tried fishing out the indices of non-NaN values and plotting those instead both for x and A variables but there still seems to be some interpolation. Please help.
Many thanks,
Cyrus
5 件のコメント
DGM
2021 年 10 月 5 日
If you really want to remove those isolated points for a particular reason, I suppose you could.
load('sample_data.mat');
m = ~isnan(sample_data);
m = bwareaopen(m,2); % remove runs shorter than 2 samples
sample_data(~m) = NaN;
subplot(2,1,1)
plot(sample_data)
subplot(2,1,2)
plot(sample_data,'o:')
This uses bwareaopen() from IPT, but I'm sure there are other ways. I'm more familiar with image processing tools than signal processing tools, so that's the hammer I'm going to use.
採用された回答
DGM
2021 年 10 月 5 日
Reposted from comments:
Without knowing what the data looks like, I imagine that not all values are NaN. Markers will be visible for lone points flanked by NaN, whereas a line-only plot shows nothing. Consider the example:
A = rand(1,10);
B = rand(1,10);
B(2:2:end) = NaN; % every other point is NaN
subplot(1,2,1)
plot([A B A])
subplot(1,2,2)
plot([A B A],'o:')
0 件のコメント
その他の回答 (1 件)
Mathieu NOE
2021 年 10 月 5 日
Removing the NaN data will not do any harm on the dot plot, but the solid line will not have the gap anymore
% fix ?
B = A;
B(isnan(A)) = [];
x(isnan(A)) = [];
figure,plot(x,B, '-k')
figure,plot(x,B, 'kx')
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!