How to plot multiple vertical lines

11 ビュー (過去 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 Center および File ExchangeGraphics Object Properties についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by