How to plot data in vertical lines

조회 수: 10 (최근 30일)
Tomas Carvalho
Tomas Carvalho 2022년 10월 20일
댓글: Tomas Carvalho 2022년 10월 20일
I want to produce a plot like the one drawn below:
Where I have two values of y for each value of x:
x=[1 2 3 4]
y1=[3 2 3 1]
y2=[4 6 7 3]
What is the best strategy to accomplish this?
Right now I believe the only way to achieve this result by doing something like the folowing piece of code.
However this is not pratical, and I'll have to write some more code, including some for loops, to deal with the original data. Costumizing all the line properties seems a painfull task as well.
plot([1 1],[3 4 ])
hold on
plot([2 2],[2,6])
plot([3 3],[3,7])
plot([4 4],[1,3])
xlim([0,5])
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2022년 10월 20일
You can use for loop. The colors of the line will be chosen at random, so there's a chance they might be similar or overlap with each other.
x=[1 2 3 4];
y1=[3 2 3 1];
y2=[4 6 7 3];
y=[y1;y2]';
for i=1:numel(x)
plot([x(i) x(i)],y(i,:),'o-')
hold on
end
xlim([0 5])
ylim([0 8])

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2022년 10월 20일
However this is not pratical
Why not? It can certainly be shortened from what you have, though:
x=[1 2 3 4];
y1=[3 2 3 1];
y2=[4 6 7 3];
h=plot([x;x],[y1;y2]); axis padded
set(h,'Marker','o')
  댓글 수: 3
Matt J
Matt J 2022년 10월 20일
편집: Matt J 2022년 10월 20일
Use the line handles:
x=[1 2 3 4];
y1=[3 2 3 1];
y2=[4 6 7 3];
lengths=y2-y1;
h=plot([x;x],[y1;y2],'-o'); axis padded
set(h(lengths>2),'Color','green')
set(h(lengths<=2),'Color','red')
Tomas Carvalho
Tomas Carvalho 2022년 10월 20일
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by