Matlab: discontinuous plot

3.7k Views Asked by At

I want to make a plot that discontinues at one point using Matlab.

This is what the plot looks like using scatter: enter image description here

However, I would like to the plot to be a smooth curve but not scattered dots. If I use plot, it would give me:

enter image description here

I don't want the vertical line.

I think I can break the function manually into two pieces, and draw them separately on one figure, but the problem is that I don't know where the breaking point is before hand.

Is there a good solution to this? Thanks.

2

There are 2 best solutions below

2
On BEST ANSWER

To find the jump in the data, you can search for the place where the derivative of the function is the largest:

[~,ind] = max(diff(y));

One way to plot the function would be to set that point to NaN and plotting the function as usual:

y(ind) = NaN;
plot(x,y);

This comes with the disadvantage of losing a data point. To avoid this, you could add a data point with value NaN in the middle:

xn = [x(1:ind), mean([x(ind),x(ind+1)]), x(ind+1:end)];
yn = [y(1:ind), NaN, y(ind+1:end)];
plot(xn,yn);

Another solution would be to split the vectors for the plot:

plot(x(1:ind),y(1:ind),'-b', x(ind+1:end),y(ind+1:end),'-b')

All ways so far just handle one jump. To handle an arbitrary number of jumps in the function, one would need some knowledge how large those jumps will be or how many jumps there are. The solution would be similar though.

0
On

you should iterate through your data and find the index where there is largest distance between two consecutive points. Break your array from that index in two separate arrays and plot them separately.