File Exchange

image thumbnail

hline and vline

version (3.27 KB) by Brandon Kuczenski
Draws 'low-impact' horizontal or vertical lines on the current axes


Updated 09 Nov 2001

View License

Editor's Note: Popular File 2019

This file was a File Exchange Pick of the Week

These functions draw horizontal or vertical lines at specified places on the current axes. The axes are automatically held for the drawing of the lines, and restored to their previous 'hold' states afterwards. THe HandleVisibility of the new lines is "off" so that they don't appear on legends. (Incidentally, this also makes them invisible to moveplot.m and addplot.m) Objects with HandleVisibility set to off can be "found" by setting the root object's "ShowHiddenHandles" property to "on". Also, if an output argument is specified, the function will return the handle to the otherwise 'hidden' object.

Further arguments allow the user to set the linetype and color of the line (default is 'r:'), and to add labels. For example:

h = vline(42,'g','The Answer')

will return a handle to a vertical line at x=42 on the current axes, colored green, and with a text label "The Answer" on the axes close to the line, and in the same color of the line.

hline and vline both accept vector inputs.

Cite As

Brandon Kuczenski (2021). hline and vline (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (131)

Robert Feldmann

Xingwang Yong

Jorge Erazo Aux

laurent jalabert

is it possible to add a "delta" as input in order to shift the position of the text on the y axis ?
I am trying to use it on subplot of loglog plot using tight_subplot function, but the actual vline text position is shifted in x for unknown reason (probably due to tight_subplot). I add 2 vlines at different x position, but the text of those 2 vlines are surimposed. So I think that adding a delta as input such as hhh=vline(x,in1,in2,delta), can help to shift or not (delta=0) the position of the annotation.
I am using Matlab 2016b (and I know that from 2018, there is a function in Matlab for this but I don't have recent version)

% Example of code modification of vline :
if xunit<0.5
text(x+1e-2*xrange,delta + y(1)+ 0.1*(y(2)-y(1)),label,'color',get(h,'color'))
text(x-5e-2*xrange,delta + y(1)+0.1*(y(2)-y(1)),label,'color',get(h,'color'))

Parisa Zadeh

Vasanth C

Far Bod

very useful! thanks


Excellent. Thank you!

Naufal Praditya

Everett Snieder

Doesn't plot full length of graph, no clue why. Simpler to just use plot(xlim,[0,0],HandleVisibility,'off')

Aron Aertssen

Super awesome!


Xiaotian Zhang

Darya Dubava

Thank you so much! It was very helpful.

Sebastian Rask

tanveer haq

Raquel Felix

Erin Winkler

Han-seok Ryu

Shabnam Nikbakhsh

James Niemeyer

To the people who wish it could do different line widths: an easy solution is to go to line 84 and add 'linewidth',2) arguments to the plot command. A non-hardcoded solution would be to add a variable to the function, "LW" and then adjust the cases (because nargin changes) and put that variable in the 84th line where the plot is called

Prithvi Pani

Great simplification. Thanks!!

Ramez hamid


Shay Moshe

M. Saad

Thanks alot

Quentin Dragomir

Oleg Boiko

Samuel Triebels

Work well thanks



Joaquin Saldain

Cannot use latex interpreter in label

Bucks Lin

very helpful!! Wish having a new version of line RGB-color enable. thanks!


Josh Beers

Robbe Louage

Yuanqi Wang

Nice and efficient!

Michael Steinbuechler

Naja Nielsen

Daniel Borrero

Morten Salvesen

Madina Makhmutova

Hoang-Hiep Le


Mehmet E. Yavuz

Ivan Litvinov

Taotao Zhou

Juliana Berthold


Nicolas Dubois

Asif Shahriar Nafi

Thanks a lot

Till Schnabel

Is it possible to change the size of the text?

Till Schnabel

Kunal Tiwari

Adaeze Adigwe

Matthias Wassenberg

Thanks, you make my day!!

Katharina Moritz

Emanuele Galiffi

julen otegi

Paul Kane

Jin Tan

Misaki Soma

Nathan Kennedy

I required a lot of lines on my graph, the labels overlap each other. Any fix to this?

Andrei Bosco


Why is this not part of stock Matlab?

Michael Stritt

Thanks, I use it all the time for my work, adjusted it a little to change the thickness etc. but it's really nice

Clemens Schlegel

Works fine but doens't work with scatterplot, only a point is drawn at the bottom when using with scatterplot.

Lakis Katabos

Frederico Marc

Thanks! The function is very helpful. With subplot it is not working 100% yet.

Brij Bhushan


It would be nice to be able to draw black lines. Could not figure out how.
Otherwise amazing tool!

Geert Kapteijns

I had a problem with subscripts of labels partially disappearing into the line. Here's how I fixed it:


Aria Ardalan

Mingwei Sun

Veronika K

Great, thank you!


Alexander Ruesch

Leandro Sousa

Very easy.
Thanks for doing this!


easy to use! powerful

mima zebouchi

plz i have a probleme in sliceomatic fonction
and also Undefined function or method 'BrainPartition' for input arguments of type 'char'.


I actually cant live without this function. It is so easy to use much easier than the alternatives.

Thanks for doing this!


any idea how to change to other segmentation algorithm ?..


Very nice.

Only issue is that the line is only applied within the visible axes. For example I am applying the line within a GUI, if you zoom out of the plot then you need to reapply.



Simple and they work great.

Yuri K

It would be nice to be able to use parameter name-value pairs as for line objects. For example, I often need 'LineWidth'. Can do it as a separate set statement, but one-liner would be better.

Nicolas Beuchat

Use it all the time!





Exactly what I wanted !


Great functions! Just what I need
It will be better if the location of text label can be arbitrarily specified.


I meant to say...very helpful tool. Slight adjustment to code wrt linewidth etc to suit my personal taste/need, otherwise good job.




Akshay B

Very helpful and simple. Thanks!
I just made a minor change to the 'vline' function (to make the labels vertical):
and changed:



I could not find these two in the Matlab stand function list. They are handy and should be standard functions. Any one know the reason why NOT? Excellent job, Brandon!


Quick, simple and effective. Excellent.


handy little tool



Good job!!! Very good


Storm Zhang

This really saves me a lot of time. Matlab should put these two small programs into the standard library.


Thanks - this saves a lot of time and trouble!


Great tool, thanks for submitting!

Ben Kang

Very nice.

nimat r

Thanks a lot.It's very nice.

Greg L

Works great for linear axes. The location of the label is off if it has logrithmic axes.

Josiah Haswell

Awesome! At least sizeof(long) times better than doing it the standard way.

John D'Errico

Very nice. My only (minor) comment would be it might be nice to control the location of the annotation text. I do agree that this is functionality that should have always been in matlab.

By the way, if anyone is worried, it works on a mac also. This is not system specific code.

Simon B

Great tool - one enhancement would be to allow for restricting the range of the lines.

Felix Totir

Should be Matlab standard functions.

carlo murgia

Chris H

perfect for what i needed.

Andy N.

Very Handy

Dimitri Shvorob

When I use VLINE, and later resize the axis with AXIS, vertical line's limits remain unadjusted.

Krishna Pillai

Nice handy function. Good job

Min Poh


James Tedd

very handy

Kedar Patel


Handy lil' time saver.

Mike Jennings

This is an excellent, easy to use tool to add a reference line in your plot figure. Can also be customized for color and linetypes. Good Job.

Stephen Sester

This function meets the common need for drawing simple horz and vert lines on plots. Very nice addition to my tools; should be incorporated into future plot revision. Thank you for taking the time to share your work.

Nahur Fonseca

thanks for writing this.
It's amazing how matlab runtime library did not have such a function built-in.

Wolfgang Stiegmaier

Excellent, easy to use, very effective, good work!

confidential confidential

fast and easy!


it's very handy and useful!!! Thanks:)

Larry Friese

Very nice, but I get two extra data elements in the plot legend? Is there a way around that?

MATLAB Release Compatibility
Created with R12
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!