plot several X=const
Show older comments
Hello,
I have a vector G (with 120 values).
I'd like to plot a vertical line for each value of this vector in a graph having an other curve. How can I do this?
thank you
Answers (1)
Image Analyst
on 17 Sep 2014
Perhaps the stem() function?
g = rand(1, 10);
plot(g, 'ro-', 'LineWidth', 2);
hold on;
stem(g);

4 Comments
José-Luis
on 17 Sep 2014
x = 1:100;
y = cos(x .* 2 .* pi ./100);
X = rand(1,10) .* 100;
Y = interp1(x,y,X);
plot(x,y,'r-','Marker','*');
hold on
stem(X,Y)
Image Analyst
on 17 Sep 2014
Maxime's "Answer" moved here since it's not an answer to her original question:
ok, thank but it is not that I want exactely:
I mean:
I have a Y vector comprising a set of values (eg Y = (2.5, 34, 65, -12, etc ...) I have another G vector comprising a set of values (eg G = (12.5, 5.789, 4.6102, 21.09, etc ...).
I would like to draw on the same graph the curve defined by the vector Y, and, the vertical lines of equation X = 12.5, X = 5.789, X = 4.6102, X = X 21.09 = ... etc (the values defined by my G vector...
How can I do ?
!!!! Y and G have not the same length
Image Analyst
on 17 Sep 2014
Edited: Image Analyst
on 17 Sep 2014
How about this:
Y = [2.5, 34, 65, -12]
G = [12.5, 5.789, 4.6102, 21.09]
plot(Y, 'ro-', 'LineWidth', 4);
grid on;
hold on;
stem(G, max(Y)*ones(1, length(G)), 'Marker', 'none', 'LineWidth', 4);

Note: Y and G don't have to have the same number of elements. ALso, you have not defined an X for the Y values so I'm just assuming X is the index number. For the "G" lines, I'm having them go just from 0 up to the max Y value since you gave no indication of where the lines start or stop.
Image Analyst
on 17 Sep 2014
Another option to go the full height of the graph is to use line() function:
Y = [2.5, 34, 65, -12]
G = [12.5, 5.789, 4.6102, 21.09]
plot(Y, 'ro-', 'LineWidth', 4);
grid on;
hold on;
yl = ylim();
for k = 1 : length(G)
x = G(k);
line([x, x], yl, 'Marker', 'none', 'LineWidth', 4);
end

Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!