File Exchange

image thumbnail

hline and vline

version 1.0 (3.27 KB) by

Draws 'low-impact' horizontal or vertical lines on the current axes

66 Ratings



View License

Editor's Note: 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.

Comments and Ratings (75)

Thanks, you make my day!!

julen otegi

Paul Kane

Jin Tan

Misaki Soma

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?

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

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

Lakis Katabos

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

Brij Bhushan


Oli (view profile)

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

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


Kevin (view profile)

Aria Ardalan

Mingwei Sun

Veronika K

Great, thank you!


PPM (view profile)

Leandro Sousa

Very easy.
Thanks for doing this!


YU GU (view profile)

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'.


Daniel (view profile)

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

Thanks for doing this!


iza (view profile)

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


Andy (view profile)

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

Yuri K (view profile)

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

Nicolas Beuchat (view profile)

Use it all the time!


bill (view profile)




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.


Qiang (view profile)


Jimmy (view profile)


Matteo (view profile)

Akshay B

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


Marit (view profile)


John (view profile)

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!


Pete (view profile)

Quick, simple and effective. Excellent.


Kevin (view profile)

handy little tool


Matteo (view profile)


N/A (view profile)

Good job!!! Very good


N/A (view profile)

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
MATLAB 6.0 (R12)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video