MATLAB Answers

Adding vertical line to plot?

조회 수: 8,509(최근 30일)
Philip 2011년 2월 25일
댓글: Roberto Chang 2021년 8월 23일
Hi there,
Can anyone please tell me how I can add a vertical line to my plot at a specified sample point? For example, I have a a 1x41 vector of intensity values, and I would like to add a vertical line on the center sample (sample number 21).
Many thanks!
  댓글 수: 1
Paulo Silva
Paulo Silva 2011년 2월 25일
hold on
SP=1; %your point goes here
line([SP SP],get(hax,'YLim'),'Color',[1 0 0])

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

채택된 답변

Michelle Hirsch
Michelle Hirsch 2016년 1월 29일
편집: Michelle Hirsch 2020년 4월 2일
Woohoo - this is built into MATLAB now, as of R2018b! You can use xline and yline to create lines with constant x or y values respectively.
Basic usage couldn't be much easier:
If you are on older releases, another option is hline and vline from the File Exchange:
  댓글 수: 8
Roberto Chang
Roberto Chang 2021년 8월 23일
Hello all! do you know if this magical (awesome) feature can be done in Z axis for bar3?

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

추가 답변(10개)

Muhammad 2014년 7월 8일
line([x x], [y1 y2]); is the easy command;
  댓글 수: 4
Christian Sanchez
Christian Sanchez 2020년 5월 8일

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

carolina franco
carolina franco 2017년 10월 26일
편집: MathWorks Support Team 2018년 11월 8일
You can plot a horizontal or vertical line using the “plot” function with this pattern:
- Horizontal line:
plot([x1 x2],[y y])
- Vertical line:
plot([x x],[y1 y2])
For example, plot a vertical line at x = 21. Set the y values using the y-axis limits of the axes.
y = ylim; % current y-axis limits
plot([21 21],[y(1) y(2)])
As Steven suggested, starting in R2018b, you can use the “xline” and “yline” functions instead. For more information, see:
  댓글 수: 4
Rasmus Ringsborg Nielsen
Rasmus Ringsborg Nielsen 2021년 3월 11일
Thank you so much, works perfect!!

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

Mark 2013년 3월 12일
편집: Mark 2013년 3월 12일
Probably the simplest way:
Choose the x-value where you want the line "xval." Choose the minimum y value to be displayed on your graph "ymin" and the maximum y value to be displayed on your graph "ymax."
Flaws with this method: probably will look silly if you use '-x' or '-.', these mark your specific points on the line, but you'll only have two (at least they're endpoints).

Steven Lord
Steven Lord 2018년 11월 1일
If you're using release R2018b or later, use the xline or yline functions to create lines with constant x or y values respectively.
  댓글 수: 1
Gary Bikini
Gary Bikini 2019년 4월 26일
Best answer!

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

the cyclist
the cyclist 2011년 2월 25일
One way:
x = rand(1,41);
y = 1:41;
line([x(21) x(21)],[0 41]);
set(gca,'YLim',[0 41])

James 2014년 3월 28일
편집: James 2014년 3월 28일
There is an excellent answer over on repeated below for convenience. ---
There exist an undocumented function graph2d.constantline:
plot(-2:5, (-2:5).^2-1)
%# vertical line
hx = graph2d.constantline(0, 'LineStyle',':', 'Color',[.7 .7 .7]);
%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
  댓글 수: 2
Ben 2016년 9월 9일
@Steven That's because undocumented features can be removed at any time, as this feature was.

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

Pedro Luis Camuñas García-Miguel
Maybe it is a bit late but I want to contribute, there is a really easy way to add vertical and horizontal lines, you just have to use a hold and then overlap them over the main plot.
Before declaring the original plot, add a hold on to ensure it will retain both plots, then plot the lines, with this structure:
hold on;
plot(the main function)
plot([x x],[0 y_max]) % Vertical Line
plot([o x_max],[y y]) % Horizontal line
x: location on horizontal axis where you place the vertical line.
y: location on vertical axis where you place the horizontal line.
x_max: point where you want the vertical line to end.
y_max: point where you want the horizontal line to end.
I hope this was useful to whoever consults this page.
  댓글 수: 2
Pedro Luis Camuñas García-Miguel

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

Jos (10584)
Jos (10584) 2014년 7월 8일
You might also be interested in GRIDXY on the File Exchange:

Julian Williams
Julian Williams 2019년 2월 9일
Small additional suggestion, say you want to label your line in the legend so that it has some meaning, or take advantage of some of the easy to use options in plot, then using "hold", the ylim from the current axis and the "repmat" is very useful. You can also make multiple vertical lines with some spacing using this technique.
% make some sort of illustration
T = 1000;
A = 0.7;
h = [];
Y = cumsum(sqrt(0.05).*randn(T,1));
X = (1:T)./T;
I = find(X>A);
Y(I) = Y(I(1));
h(1) = plot(X,Y,'-k','linewidth',2);
hold on
dims = get(gca,'ylim');
yy = linspace(dims(1),dims(2),100);
xx = repmat(A,1,100);
h(2) = plot(xx,yy,':r','linewidth',2);
dims = get(gca,'xlim');
xx = linspace(dims(1),dims(2).*A,100);
yy = repmat(Y(I(1)),1,100);
h(3) = plot(xx,yy,':b','linewidth',2);
grid on
G = legend(h,'Particle Motion','Stopping Point','Stopped Value');
Just a thought.

Adrian Peters
Adrian Peters 2020년 2월 8일
Sorry, but what does (-2:5).^2-1 do? I dont know, how to calculate the ^2-1.
  댓글 수: 2
Adrian Peters
Adrian Peters 2020년 2월 8일
Now it makes sense to me! Thank you a lot!

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

Community Treasure Hunt

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

Start Hunting!

Translated by