Plot function behave differently when only using line versus only using markers when plotting data with NaN values

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 Commenti

Hello
no difference on my R2020b - same gaps for NaN data - for both solid and dot plots
clc
clearvars
close all
x = (1:22);
A = [(1:10) NaN NaN (11:20)]
figure,plot(x,A, '-k')
figure,plot(x,A, 'kx')
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:')
Hi Mathieu and DGM,
Thank you for your responses.
DGM, I think you have identified my issue, I looked carefully into the data and there are some lone points between the NaNs. For what it's worth, I am attaching it.
Many thanks,
Cyrus
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.
Thank you so much for the insight, DGM. I agree, I will have to explore other ways as I do not have the IPT installed.
By the way, you pointing out what I missed is sufficient enough to get me through. I'm aware we are on the comment section so I cannot choose your first suggestion as an 'accepted answer' - are you happy for me to close this question by only acknowledging you here in the comments section? Or would you prefer to re-post your first suggestion as an answer so I can select it as 'accepted answer'?

Accedi per commentare.

 Risposta accettata

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:')

Più risposte (1)

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')

2 Commenti

Thank you for this suggestion Mathieu, although I was actually trying to achieve a cleaner set of data with the NaNs ignored using just the markers, this will be a useful technique for me in the future if I need to do it this way.

Accedi per commentare.

Prodotti

Release

R2021a

Tag

Richiesto:

il 5 Ott 2021

Risposto:

DGM
il 5 Ott 2021

Community Treasure Hunt

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

Start Hunting!

Translated by