Plot function behave differently when only using line versus only using markers when plotting data with NaN values
2 views (last 30 days)
Show older comments
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 Comments
DGM
on 5 Oct 2021
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.
Accepted Answer
DGM
on 5 Oct 2021
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 Comments
More Answers (1)
Mathieu NOE
on 5 Oct 2021
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')
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!