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:
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
.
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!