How to plot multiple vertical lines
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
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.
0 个评论
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])
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Properties 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!