Adding vertical line to plot?
Mostrar comentarios más antiguos
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!
3 comentarios
Paulo Silva
el 25 de Feb. de 2011
Editada: Jan
el 29 de En. de 2018
fig=figure;
hax=axes;
x=0:0.1:10;
hold on
plot(x,sin(x))
SP=1; %your point goes here
line([SP SP],get(hax,'YLim'),'Color',[1 0 0])
Benita
el 26 de Feb. de 2023
(4x3+y3 )dx+(3xy²-8y3)dy=0
Walter Roberson
el 26 de Feb. de 2023
Respuesta aceptada
Más respuestas (11)
Muhammad
el 8 de Jul. de 2014
40 votos
line([x x], [y1 y2]); is the easy command;
4 comentarios
Ryuji Segawa
el 29 de Sept. de 2016
you are a genius!
Bin Miao
el 5 de Dic. de 2017
Thanks!
Claire Flashman
el 11 de Feb. de 2018
Thank you!
Christian Sanchez
el 8 de Mayo de 2020
Genial
carolina franco
el 26 de Oct. de 2017
Editada: MathWorks Support Team
el 8 de Nov. de 2018
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 comentarios
Junayed Chowdhury
el 30 de En. de 2018
Editada: Stephen23
el 19 de Mzo. de 2018
This one works fantastically...Thanks a lot :D cheers!!
Camilo Malagon Nieto
el 19 de Mzo. de 2018
Editada: Camilo Malagon Nieto
el 23 de Abr. de 2018
This is AMAZING!!! because it makes the line automatically covering the data area of the plot. So I do not need to do extra work of finding where the line should start and should end. It works for several different plots that had diferent y-axis ranges.
Edward Manson
el 28 de Ag. de 2019
Editada: Edward Manson
el 28 de Ag. de 2019
What an absolute god, thankyou
Rasmus Ringsborg Nielsen
el 11 de Mzo. de 2021
Thank you so much, works perfect!!
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."
x=[xval,xval];
y=[ymin,ymax];
plot(x,y)
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
el 1 de Nov. de 2018
8 votos
1 comentario
Gary Bikini
el 26 de Abr. de 2019
Best answer!
the cyclist
el 25 de Feb. de 2011
One way:
figure
x = rand(1,41);
y = 1:41;
plot(x,y,'r.');
line([x(21) x(21)],[0 41]);
set(gca,'YLim',[0 41])
There is an excellent answer over on http://stackoverflow.com/a/8108766/1194420 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]);
changedependvar(hx,'x');
%# horizontal line
hy = graph2d.constantline(0, 'Color',[.7 .7 .7]);
changedependvar(hy,'y');
5 comentarios
Steven
el 6 de Abr. de 2015
Why is there no documentation on this function? It works great but it is difficult to motivate putting undocumented methods in code that I share with others.
Ben
el 9 de Sept. de 2016
@Steven That's because undocumented features can be removed at any time, as this feature was.
Adrian Peters
el 8 de Feb. de 2020
Movida: DGM
el 25 de Feb. de 2023
Sorry, but what does (-2:5).^2-1 do? I dont know, how to calculate the ^2-1.
Walter Roberson
el 8 de Feb. de 2020
Movida: DGM
el 25 de Feb. de 2023
-2:5 is the list of values -2 -1 0 1 2 3 4 5 . The .^2 squares each element of the list giving you 4 1 0 1 4 9 16 25 . Then you subtract 1 from each giving you 3 0 -1 0 3 8 15 24
Adrian Peters
el 8 de Feb. de 2020
Movida: DGM
el 25 de Feb. de 2023
Now it makes sense to me! Thank you a lot!
Pedro Luis Camuñas García-Miguel
el 13 de Abr. de 2018
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
Being:
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 comentarios
Walter Roberson
el 23 de Abr. de 2018
If you use line() instead of plot() then you do not need the "hold". line() is one of the primitives that always adds to the current plot; it is the "high level plotting routines" that clear the current axes before plotting and need the "hold"
Pedro Luis Camuñas García-Miguel
el 8 de Mayo de 2018
Thanks!
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.
figure
% 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');
set(G,'location','best','interpreter','latex');
Just a thought.
Guy Cohen
el 22 de Nov. de 2022
You can use arrayfun
x=1:180;
figure;plot(x,sind(x)); %-- your graph
vLines=[20 40 50 120];%-- vector of lines to plot
hold on; arrayfun(@xline,vLines);%-- plot vertical lines
3 comentarios
You could, but xline accepts a vector of values, so you can just
x=1:180;
plot(x,sind(x)); %-- your graph
xline([20 40 50 120])
Guy Cohen
el 22 de Nov. de 2022
Agree, but xline accepts a vector only in the latest versions
Walter Roberson
el 11 de Dic. de 2024
It looks like xline starts handling vectors in either R2020a or R2020b (not sure which at the moment.)
Jos (10584)
el 8 de Jul. de 2014
0 votos
You might also be interested in GRIDXY on the File Exchange:
amit
el 27 de Feb. de 2025
%% generation of tanwave
clear all
close all
x=0:0.001:20;
a=2;
y=a*cos(x);
plot(x,y);
xlabel ("time axis")
ylabel("amplitude")
title("tanwave")
grid on
1 comentario
Walter Roberson
el 27 de Feb. de 2025
This does not generate tan wave -- it generates cosine wave.
It is not clear how this code is intended to satisfy the question of how to generate reference lines?
Categorías
Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


