How to plot a broken horizontal line (ie, a horizontal line with whitespace)
3 views (last 30 days)
Show older comments
As above, I am trying to plot segments of a horizontal line, y = 5. I have a vector called v of ones and zeros, the ones representing the line I want to plot, and zeros indicating whitespace. I've had trouble finding a formal way to put this, so I've included a picture illustrating what I mean.

2 Comments
Geoff Hayes
on 22 Feb 2018
Daniel - how do you map the dashes to the x-axis? For example, if you have
0 0 0 1 1 1 0 0 0 1 1 1
what is the equivalent x-value for each of the above? Is it
1 2 3 4 5 6 7 8 9 10 11 12
or
0.5 1 1.5 2 2.5 ...
Answers (2)
Geoff Hayes
on 22 Feb 2018
Daniel - so if your array of zeros and ones is
onOffData = logical([0 0 0 1 1 1 0 0 0 1 1 1]);
and it maps to 1, 2, 3, ... then you could do something like
xData = 1:1:length(onOffData);
yData = zeros(size(xData));
yData(onOffData) = 5;
You can then plot the data as
plot(xData,yData,'.')
and you should see the '.' wherever there is a one in your logical array. (Note the conversion to the logical array above so that we can easily set the on (1) elements to 5 in the yData(onOffData) = 5; assignment).
0 Comments
Stephen23
on 22 Feb 2018
Edited: Stephen23
on 22 Feb 2018
NaN values are not plotted, so all you need is to plot a vector with NaNs in the location that you do not want plotted:
V = [0,0,0,1,1,1,0,0,0,1,1,1];
V(V==0) = NaN
plot(V)
3 Comments
Stephen23
on 22 Feb 2018
Edited: Stephen23
on 22 Feb 2018
10,078,080 is not a trivially small number of points to plot, so perhaps one workaround would be to reduce the number of points plotted. This can be done quite simply by only plotting the start and end points of each line segment. Here is one way to do this:
V = [0,0,0,1,1,1,0,0,0,1,1,1];
X = reshape(find(diff([0,V,0])),2,[]);
X(2,:) = X(2,:)-1;
Y = [5;5];
plot(X,Y)
I have no idea how fast/slow this is in comparison to using NaN, so please try it and let me know!
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!