How to plot multiple vertical lines

조회 수: 9 (최근 30일)
Patrik
Patrik 2016년 5월 11일
답변: Walter Roberson 2016년 5월 11일
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?

답변 (2개)

Walter Roberson
Walter Roberson 2016년 5월 11일
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
Weird Rando 2016년 5월 11일
편집: Weird Rando 2016년 5월 11일
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

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by