Search code examples
matlabmatlab-figurelegendlegend-properties

Legend next to vertical line plot


In Matlab, it is possible to create a legend for a plot. Now I do not want the information in the legend box, but directly next to the line as follows:

enter image description here

(This is only an example)

For the vertical lines, I did the following:

v1 = vline(14.7,'k')
set(v1,'Color','black','LineStyle','--','LineWidth',1);

How do I add information next to the two vertical lines (just like 14.7s and 18.6s) as in the figure?


Solution

  • Use text function, e.g.

    figure();
    v1 = vline([14.7, 18.6], 'k');
    set(v1, 'Color', 'black', 'LineStyle', '--', 'LineWidth', 1);
    xlim([8,25]);
    
    text(14.8, 0.87,'\leftarrow 14.7 s');
    text(18.7, 0.9,'\leftarrow 18.6 s');
    

    enter image description here