How to avoid a vertical line at discontinuity point?
Show older comments
When I plot a function with discontinuity points, I get a vertical line at such points, as it can be seen in this simple example:
clc;
clear all;
h=@(x) (x>=3 & x<=7).*1+(3<x & x>7).*3;
x = linspace(0,10,100);
figure(1)
plot(x,h(x));
As it can be seen, there are near vertical lines at 3 and 7.
Attempting to remedy this, I tried the following:
clc;
clear all;
h=@(x) (x>=3 & x<=7).*1+(3<x & x>7).*3;
tol=0.0001;
x1=linspace(0,3-tol,1000);
x2=linspace(3,7,1000);
x3=linspace(7+tol,10,1000);
figure(2)
plot(x1,h(x1),x2,h(x2),x3,h(x3))
The vertical lines disappear alright, but it cycles through the colors. I can fix this coloring stuff, but at this point I must ask if there is a better way to deal with this, specially because in some situations the discontinuity points are not so straight-forwardly given.
4 Comments
Roger Stafford
on 21 Jan 2015
How better to handle such a discontinuity than with a vertical lines? That tells you exactly what is happening.
Zoltán Csáti
on 21 Jan 2015
It's true, but if you run his example, then you see that is not exactly a vertical line so if someone looks at the graph, he/she will not know if it is a discontinuity or a sharp change.
Ian Hunter
on 6 Feb 2021
%assuming you are plotting yy vs xx
%set max jump u consider a discontinuity
tol = 1;
yyDiffs = diff(yy);
yyDiffs = [0; yyDiffs;]; %make same length as yy in lazy way
diffintolIndices = abs(yyDiffs) <tol;
xx(~diffintolIndices)=NaN;
yy(~diffintolIndices)=NaN;
% :}
Nate Sutton
on 24 Aug 2022
Thanks @Ian Hunter this idea of using NaNs was a big help to me. I tried many approaches and this way at last worked.
Accepted Answer
More Answers (4)
Jit
on 4 Apr 2016
If you know where the discontinuities are, you can separate them with NaNs. I have to do this to mark bad data in time-series analysis, matlab will plot the line as separated segments.
figure
time = 0:0.1:2;
freq = 2;
time = 0:0.01:2;
data = sin(2*pi*freq*time);
subplot(2,1,1);
plot(time,data);
grid on; grid minor;
ind_bad = [50:100];
data(ind_bad) = NaN;
subplot(2,1,2);
plot(time,data);
grid on; grid minor;

Zoltán Csáti
on 21 Jan 2015
0 votes
I advise you to handle the subintervals distinctly and plot them independently from each other. In that case no vertical (more precisely this is not a vertical line, since it links two consecutive data points) line appears. It could be nicely done with OOP. Create an interval class. If you have k number of subintervals, then create k instances of the interval class. After it you can call the plot function for them as an object array.
Another option is simply to plot without joining the points. If your sampling is dense enough, the continuous parts of the locus will still look as though they are joined. Compare the red and blue curves:
g=@(x) sign(x-1).*exp((x-1));
t=linspace(-2,2,2001);
plot(t,g(t),'r*', t,g(t),'b-')
Walter Roberson
on 24 Aug 2022
0 votes
If you use fplot() then you can use 'showpoles', 'off' to avoid vertical lines at discontinuities
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!