Azzera filtri
Azzera filtri

Draw vertical lines on a plot

5 visualizzazioni (ultimi 30 giorni)
Curious Mind
Curious Mind il 3 Dic 2019
Commentato: Luna il 4 Dic 2019
Hi,
I have a plot and I want to draw several vertical lines (about 40 of them) at specific locations on the plot. How do I go about it? Any code that I can put all those locations in and automatically mark those regions of the plot with vertical lines for me? Thanks!

Risposta accettata

Luna
Luna il 3 Dic 2019
Modificato: Luna il 3 Dic 2019
There is a built-in function for that.
For vertical lines use xline and for horizonal lines use yline.
Here is sample code:
regions_to_be_marked = randn(40,1); % array of the points you want to plot vertical line (x axis values)
x_data = randn(100,1); % x data to plot
y_data = randn(100,1); % y data to plot
hFig = figure; % figure handle
hAxes = axes(hFig); % axis handle
hPlot = plot(hAxes,x_data,y_data,'LineStyle','none','Marker','*'); % your actual plot
hold on; % holds current plot
% plot each region in for loop
for i = 1:numel(regions_to_be_marked)
xline(hAxes, regions_to_be_marked(i),'LineWidth',2,'Color','red');
end
  2 Commenti
Curious Mind
Curious Mind il 3 Dic 2019
Thank you!
Luna
Luna il 4 Dic 2019
Your welcome :)

Accedi per commentare.

Più risposte (1)

the cyclist
the cyclist il 3 Dic 2019
If you have R2018b or later, you can use the xline command.
If you have an earlier version, you can use the line command (which is just a bit more working in defining the endpoints).
  2 Commenti
Curious Mind
Curious Mind il 3 Dic 2019
Thank you for the information. I have seen the xline function but if I have say 40 regions I want to mark, I will have to do it for each one of them. Is there a way to enter all 40 regions and get those regions marked at the same time?
Luna
Luna il 3 Dic 2019
Check my answer above, I made a sample plot for your case.

Accedi per commentare.

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by