How to plot vertical lines for each data point?

I have a line graph plot which is giving me a correct figure looking like the following:
However, my task is to recreate a zonation simulating the following figure:
I therefore need to add vertical lines to the plot at each data point along the x axis.
Any help would be greatly appreciated.

 採用された回答

Steven Lord
Steven Lord 2020 年 3 月 11 日

3 投票

Combine a plot plot (or a line plot) and a stem plot.
x = 0:10;
y = x.^2;
plot(x, y);
hold on;
stem(x, y, 'Marker', 'none');

1 件のコメント

Victoria Wilson
Victoria Wilson 2020 年 3 月 11 日
Thank you Steven, this works great!

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

その他の回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 3 月 11 日
編集済み: Ameer Hamza 2020 年 3 月 11 日

0 投票

Check this example
% Example data
x = logspace(0,1,30);
y = log(x);
% plot original line
plot(x,y);
hold on;
% plot vertical lines
line_coordinates = [x' x' zeros(size(x')) y'];
line_coordinates = mat2cell(line_coordinates, ones(numel(x),1), [2 2])';
line_coordinates(3,:) = {'k'};
plot(line_coordinates{:})

3 件のコメント

Victoria Wilson
Victoria Wilson 2020 年 3 月 11 日
編集済み: Victoria Wilson 2020 年 3 月 11 日
My x axis is not consistantly placed
I have 10 data points on the x axis which are as follows:
950:983:1002:1004:1031:1047:1077:1098:1127:1160
So I tried to adapt your suggested code to account for this by replacing the x with my xplot and the y with my y plot. This has not worked and I'm unsure what else to try. Do you have any more suggestions?
Ameer Hamza
Ameer Hamza 2020 年 3 月 11 日
Check Steven's answer. It mentions a cleaner solution.
Victoria Wilson
Victoria Wilson 2020 年 3 月 11 日
Thank you for your help Ameer!

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

カテゴリ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by