How to plot multiple vertical lines

4 views (last 30 days)
Patrik on 11 May 2016
Answered: Walter Roberson on 11 May 2016
I have 3 vectors
x=[1;1;4;4;5;5;5];
ystart=[3;33;7;23;5;15;27];
ystop=[32;45;15;45;13;26;42];
For this example I want to draw 7 (7 rows) vertical lines.
For x=1 I want to plot 2 vertical lines with y values 3 to 32 and 33 to 45
For x=4 I want to plot 2 vertical lines with y values as well, 7 to 15 and 23 to 45
For x=5 I want to plot 3 vertical lines with y values 5 to 13, 15 to 26 and 27 to 42
I can of course do a loop but in my real case it is something like 100000 X values and about 5 vertical lines per x value
Is there any version of bar or plot I can use directly?

Walter Roberson on 11 May 2016
plot([x.';x.'],[ystart.';ystop.'])
The above will create one line object for each pair. For large vectors this would be inefficient. It does, however, give the possibility of coloring each individually.
tx = [x.';x.';nan(1,length(x))];
ty = [ystart.';ystop.';nan(1,length(x))];
plot(tx(:),ty(:))
This creates one line object, total, and is much more efficient. It cannot, however, have the segments colored individually.

Weird Rando on 11 May 2016
Edited: Weird Rando on 11 May 2016
x=[1;1;4;4;5;5;5];
ystart=[3;33;7;23;5;15;27];
ystop=[32;45;15;45;13;26;42];
x = [x x];
nloop = size(x,1);
figure;
hold on;
for ii = 1:nloop
plot(x(ii,:),[ystart(ii,1) ystop(ii,1)])
end
axis([0 6 -1 46])
you could go with the bar function