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

조회 수: 3(최근 30일)
Cyrus Espinoza
Cyrus Espinoza 2021년 10월 5일
답변: DGM 2021년 10월 5일
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
Cyrus Espinoza
Cyrus Espinoza 2021년 10월 5일
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'?

댓글을 달려면 로그인하십시오.

채택된 답변

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

추가 답변(1개)

Mathieu NOE
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')
  댓글 수: 2

댓글을 달려면 로그인하십시오.

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by