Clear Filters
Clear Filters

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
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?

Answers (2)

Walter Roberson
Walter Roberson on 11 May 2016
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))];
This creates one line object, total, and is much more efficient. It cannot, however, have the segments colored individually.

Weird Rando
Weird Rando on 11 May 2016
Edited: Weird Rando on 11 May 2016
x = [x x];
nloop = size(x,1);
hold on;
for ii = 1:nloop
plot(x(ii,:),[ystart(ii,1) ystop(ii,1)])
axis([0 6 -1 46])
you could go with the bar function


Find more on Graphics Object Properties 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!