4

Is there is elegant way to connect the line between the nearest points in scatter plot?

The reason I ask is because plot will connect the line based on the 'index of row' of Y(data). Basically, it connects points in the same row in Y. However, this sometimes will result in a jump, which is due to the missing data in one row( and thus all the following data will all mislabeled and shifted by 1, and this cannot be avoided for some practical reason).

Here is a minimal example of the problem.

xrange=linspace(-2,2,100);
Y=repmat(-xrange.^2,4,1)+repmat((-4:-1)',1,100);
Y(Y<-5)=0;
for i=1:100
    [~,~,v]=find(Y(:,i));
    Y(1:length(v),i)=v;
end
Y(Y==0)=nan;
%jump due to missing data
figure;
plot(xrange,Y);
figure;
%from bare eye, we see there are four lines
for i=1:4
    scatter(xrange,Y(i,:),'b');
    hold on
end

The undesirable result by using plot is:

plot with jump

The jump is due to the missing data and this is unavoidable in practice.

However, we can see that there are actually four lines, by bare eyes, if we use scatter.

enter image description here

So what I want to achieve is to connect the nearest points but without introducing discontinuity given a imperfect data set, which is missing some data. What I can come up with is to preprocess the data before plotting but this is not always possible, because of the complicated experimental situation, which is hard to predict which data point will be missing in advance.

Any comments and answers are highly appreciated!

Jake Pan
  • 262
  • 1
  • 8
  • Is your experimental data like the `Y` you create here, or do you move the data points this way in your experimental data before plotting? Is your experimental data as smooth as this example? – Cris Luengo Feb 26 '19 at 14:21

1 Answers1

0
idx = ~isnan(Y)

plot(xrange(idx), Y(idx))

or better yet, since you know you want to get rid of everything less than -5

idx = Y < -5
plot(xrange(idx), Y(idx))
Mike C
  • 103
  • 7