Adding vertical line to plot?

2,870 ビュー (過去 30 日間)
Philip
Philip 2011 年 2 月 25 日
コメント済み: Walter Roberson 2023 年 2 月 26 日
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 件のコメント
Benita
Benita 2023 年 2 月 26 日
(4x3+y3 )dx+(3xy²-8y3)dy=0

サインインしてコメントする。

採用された回答

Michelle Hirsch
Michelle Hirsch 2023 年 8 月 28 日
編集済み: MathWorks Support Team 2022 年 4 月 28 日
Woohoo - this is built into MATLAB now, as of R2018b! 
If you are running R2018b or later, you can use the “xline” and “yline” functions. For example, create a vertical line at x=5:
xline(5)
Create a horizontal line at y=10:
yline(10)
Starting in R2021a, you can create multiple horizontal or vertical lines in one pass. For example, create vertical lines at x=1, x=2, and x=3:
xline([1 2 3])
If you are running R2018a or earlier, use 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)])
  8 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 21 日
in https://www.mathworks.com/matlabcentral/answers/2031-adding-vertical-line-to-plot#answer_207851 Michelle posted a link to a file exchange contribution that defines vline.
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
Muhammad 2014 年 7 月 8 日
line([x x], [y1 y2]); is the easy command;
  4 件のコメント
Claire Flashman
Claire Flashman 2018 年 2 月 11 日
Thank you!
Christian Sanchez
Christian Sanchez 2020 年 5 月 8 日
Genial

サインインしてコメントする。


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 件のコメント
Edward Manson
Edward Manson 2019 年 8 月 28 日
編集済み: Edward Manson 2019 年 8 月 28 日
What an absolute god, thankyou
Rasmus Ringsborg Nielsen
Rasmus Ringsborg Nielsen 2021 年 3 月 11 日
Thank you so much, works perfect!!

サインインしてコメントする。


Mark
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."
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
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:
figure
x = rand(1,41);
y = 1:41;
plot(x,y,'r.');
line([x(21) x(21)],[0 41]);
set(gca,'YLim',[0 41])

James
James 2014 年 3 月 28 日
編集済み: James 2014 年 3 月 28 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 2 月 8 日
移動済み: DGM 2023 年 2 月 25 日
-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
Adrian Peters 2020 年 2 月 8 日
移動済み: DGM 2023 年 2 月 25 日
Now it makes sense to me! Thank you a lot!

サインインしてコメントする。


Pedro Luis Camuñas García-Miguel
Pedro Luis Camuñas García-Miguel 2018 年 4 月 13 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 4 月 23 日
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
Pedro Luis Camuñas García-Miguel 2018 年 5 月 8 日
Thanks!

サインインしてコメントする。


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.
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
Guy Cohen 2022 年 11 月 22 日
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
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 11 月 22 日
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
Guy Cohen 2022 年 11 月 22 日
Agree, but xline accepts a vector only in the latest versions

サインインしてコメントする。


Jos (10584)
Jos (10584) 2014 年 7 月 8 日
You might also be interested in GRIDXY on the File Exchange:

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by