File Exchange

image thumbnail


version 2.1 (58.2 KB) by

Draw a line with an arrowhead.

145 Ratings



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week Popular File 2012 2013

ARROW (5/25/2016) Draw a line with an arrowhead.

ARROW(Start,Stop) draws a line with an arrow from Start to Stop (points should be vectors of length 2 or 3, or matrices with 2 or 3 columns), and returns the graphics handle of the arrow(s). By itself, ARROW will use the mouse to allow selection.

ARROW DEMO & ARROW DEMO2 show 3-D & 2-D demos of the capabilities of ARROW.

ARROW PROPERTIES shows the properties that can be set (length, base angle, tip angle, width, which ends have arrowheads, etc.)

Comments and Ratings (182)


It works well if you have some kind of nice axis limits. But otherwise you have to play with arrow appearance a lot..
Example: arrow([0 1e6],[1000 1e6]); - it doesnt produce an arrow one would expect..

Jack Lee

The arrow drawing works great, but before I called the function, I plotted two other curves and called the legend function to give them labels (just two labels). However, after calling legend I then call four arrows and they show up in the legend and I can't seem to exclude them. In the legend, the four arrows show up with the labels "data1", "data2", "data3", "data4". I can find no way to exclude them.

I'm using 2017a

I'm seeing some strange behavior when the renderer is painters where part of the tip angles up a little bit.

Shujaat Khan

Shujaat Khan (view profile)

Cheng Zeng

Very useful! I've had quite a bit of trouble with quiver() in the past since it's not meant to draw a shape so much as vectors, but this does the trick!

Note on axis limits: The warning "Warning: ARROW changed the axis limits when adding the arrow.
Call ARROW FIXLIMITS to reset them now." does not have a warning.identifier to allow it to be suppressed, but calling "axis(axis)" before using arrow() prevents the error from occurring.


arnold (view profile)

I'm using this soooo much, however, it would be nice if you could add the ability for the arrows to always 'face' the camera, so that they're not flat in 3d space when rotating.

Stephen Elias

Yujiao Sun

Shima Nazari

Collin Smith

Excellent documentation, and very useful packaged function!

tao lu

tao lu (view profile)

Erik Johnson

Erik Johnson (view profile)

Friedhelm, It only happens in debugging? Can you email me some code? (Email address is in the m-file; or click on my name then "view profile" then "Contact".)

Dear Erik, I am running several Matlab versions (2016a, 2016b and 2017a) and the "strange behavior" is that the arrows are distorted in both, length and shape of the arrow head. Meanwhile I noticed that calling "arrow" in pointwise fashion (in a for-loop) and debugging the code step by step is causing the problem. If I avoid debugging then the problem disappears. Many thanks. Friedhelm

Erik Johnson

Erik Johnson (view profile)

Can you describe the strange behavior? What MATLAB version? Can you provide a minimal working example to demonstrate it?

First of all, this is a great function, which I am using now for many years. Thanks! However, today I observed a strange behaviour after calling it twice (different figures) but with identical arguments and I wonder what the reason may be. The handle argument lists are identical. Any help would be highly appreciated. Thanks. Friedhelm Krumm

Alex Kotov


Paul (view profile)


looks perfect


Cebri (view profile)

Yiming Pan

Awesome work!



Fantastically good.

Yuki Hayashi


Baoning Wu

With perfect control of properties, this is exactly what I want!

Jing Lou

Nice work!


Nice Job! Works well with my oldest version of MATLAB.

Here is my line of the code:

Both tStart an tStop are numerical parameters and pre-defined.


Rick (view profile)

quan yuan

Erik Johnson

Erik Johnson (view profile)

Daisuke, I've not seen this problem before. What version of MATLAB? Can you give a minimal working example to demonstrate it?


When I drew a vertical arrow, the line got off from the arrow head (see the url for a picture). Is there a way to fix this? If not, could you tell me how I could adjust this ?

Erik Johnson

Erik Johnson (view profile)

After a long hiatus, arrow has been updated for HG2 compatibility. I _think_ most of the comments below have been incorporated.


What's going on with this error:

views = get(ax(:),{'View'});

Error in arrow (line 961)
mask = arrow_is2DXY(oldaxlims(:,1));

Thanks anyone!

Works well on (R2015b) after putting in the exact changes that Johannes recommends (2 posts down). Before that it didn't work on my version but did on other matlab installs I have at school etc., getting the same error people are talking about.


Ken (view profile)

Ooops, my bad. You must implement all of the suggestions by Justin and Brenton.


For everybody that has problems running the current arrow.m version on newer Matlab versions, please follow the corrections suggested by Justin and Brenton from 11 Nov 2014. I adapted them on Matalb R2015a and it works....

Here the suggested changes copied from their posts:

Previous: Line 423 ax = o * gca;
Update: ax = repmat(gca,narrows,1);

Previous: Line 590 curT = get(curax,'Xform');
Update: [azA,elA] = view(curax);
curT = viewmtx(azA,elA);

Brenton: Piggy-backing on Justin's edits, update with the following:

LINE 504: oldaxlims(min(find(oldaxlims(:,1)==0)),:) = [ii reshape(axl',1,6)];

LINE 960:
lims = get(ax(oldaxlims(:,1)),{'XLim','YLim','ZLim'})';

LINE 962:
mask = arrow_is2DXY(ax(oldaxlims(:,1)));

Amir Paster

It doesn't work in Matlab 2015a

>> arrow
Undefined operator '*' for input arguments of type ''.

Error in arrow (line 423)
ax = o * gca;

ankit dixit

ankit dixit (view profile)


Moses (view profile)

Thanks Justin and Brenton! I got it to work for MatLAB version 2015b following your edits.

Leonard Wayne

As an alternative, there is now the DaVinci Draw toolbox,, which uses low-level Matlab commands like plot() and patch() to draw mid-level shapes like arrows. Example syntax:

davinci( 'arrow', 'X', [0 10], ...
'Y', [0 2], ...
'Shaft.Type', 'rectangle', ...
'Shaft.Width', 1, ...
'Head.Length', 2.5, ...
'Color', 'w', ...
'EdgeColor', 'k', ...
'LineWidth', 2.5 )


Same here in 2015b, not working properly
>> arrow DEMO
Undefined operator '*' for input arguments of type ''.

Error in arrow (line 423)
ax = o * gca;

Error in arrow>arrow_demo3 (line 1273)
h1 = feval(mfilename,[axlim(1) axlim(4) 4],[-.8 1.2 4], ...

Error in arrow (line 142)


Zhigang Xu

Zhigang Xu (view profile)

Dear Author,

It seems that the ARROW cannot work with Matlab R2015a version. I got an error message as

Error in arrow (line 406)
ax = o * gca;

Can you fix it? Thanks!


Andrew Boysen

Thank you Daniel Claes - that worked for me!

Daniel Claes

It is not mine, but I found a working version it on GitHub (works for 2015a):

Good contribution. Thanks!

Jakob Sievers

Jakob Sievers (view profile)

Thank you for the preceeding comments. They helped making this otherwise excellent function work. 5 stars for the function. 0 stars for the slow updating. I strongly encourage the author update this function according to the comments made.

Min-Chi Shih

Thanks to Justin A., Brenton, and Bruce Elliott, I have got it to work as well (R2014b). This is much better than the built-in annotation.m.

Thank you for the comments - I've got it to work now too.


Erik (view profile)

Thanks to the comments of Justin A., Brenton and Bruce Elliott, I got this fantastic file working for the arrows I needed in MATLAB R2014b. Author: please update this file to make it compatible with the new graphics system.


Barbara (view profile)


Eli (view profile)

did anyone manage to make
arrow DEMO
work with 2014b? would it be possible to upload an updated working version?

Bruce Elliott

Thanks to Justin and Brenton for the recommended mods for R2014b. I have another set of related changes.

These changes will suppress warnings about use of the graphics object property "EraseMode", which is no longer used. I believe the only purpose of these lines was to temporarily change the EraseMode and then to reset it to its original value.

REMOVE LINES 1031, 1032, 1033, 1065:

oldArrowProps = {'EraseMode'};
oldArrowValue = get(H,oldArrowProps);
set(H,'EraseMode','background'); %because 'xor' makes shaft invisible unless Width>1



Piggy-backing on Justin's edits, update with the following:

LINE 504: oldaxlims(min(find(oldaxlims(:,1)==0)),:) = [ii reshape(axl',1,6)];

LINE 960:
lims = get(ax(oldaxlims(:,1)),{'XLim','YLim','ZLim'})';

LINE 962:
mask = arrow_is2DXY(ax(oldaxlims(:,1)));

Justin A.

Strike that. It works for one arrow, but not the demo.

Justin A.

I was able to get it to work in 2014b with 2 changes:

Previous: Line 423 ax = o * gca;
Update: ax = repmat(gca,narrows,1);

Previous: Line 590 curT = get(curax,'Xform');
Update: [azA,elA] = view(curax);
curT = viewmtx(azA,elA);

Yang Liu

@David, @Tobias, I have encountered the same issue.
Did you manage to resolve the problem? Thanks in advance!


David (view profile)

Love this script, but it looks like there's a fundamental incompatibility with the new 2014b graphics system, in which gca no longer returns a number. Simply running the demo (arrow DEMO) returns the error:

Undefined function 'mtimes' for input arguments of type
Error in arrow (line 423)
ax = o * gca;

Nice function, but unfortunately it doesn't accept axes handle, like plot. In same cases, like Maltab GUI, one cannot just set the axes before making the plot.


Pete (view profile)

I was a bit sceptical, but this is actually a very useful and well-documented function. Many thanks


Darien (view profile)

You are brilliant. Thank you very much for your hard work and masterful technique.


Johan (view profile)

Works nicely, but i did find a bug:
an exactly vertical arrow (from [0 0 0] to [0 0 1]) is plotted with a length of ~16 reaching far in the negative z-domain. Just type in
arrow([0 0 0],[0 0 1])
and see.

Does someone know a solution to this?

with kind regards,

Peter Caday

@Li, after reading Rajib's comment below, I saw you can change color in one function call:

h = arrow(..., 'EdgeColor', 'g', 'FaceColor', 'g');

@Tobias, I don't have 2014b, but you could try replacing line 423 with:

ax = repmat(o, narrows, 1);

After updating Matlab to 2014b, arrow.m is not working. It is stopping in line 423 with the error:

Undefined function 'mtimes' for input arguments of type ''.

Error in arrow (line 423)
ax = o * gca;

Can somebody please help me?

Terrific. Thanks.
For those of you who would like to have a small gap between the arrow and the start and end point (e.g. so as not to obscure a marker), I've written a slight update (note: not compatible with arrow.m properties directly, but you could easily adapt):

function [varargout] = my_arrow(start,stop)
arrow_gap_pc = 0.05; % defines gap size relative to size of initial arrow
[th,phi,r] = cart2sph(dx,dy,dz);
arrow_gap = arrow_gap_pc*r;
[x1,y1,z1] = sph2cart(th,phi,arrow_gap);
b = start+[x1 y1 z1];
e = stop-[x1 y1 z1];
ah = arrow(b,e);
if nargout == 1
varargout{1} = ah; end

I think this is one of the best functions I have ever stumbled upon. Thanks alot.


Yagiz (view profile)

Li Zhang

Useful! How to change the color of the arrow ?


Alan (view profile)

ammar al jodah

Jo Williams

Very useful, thanks. The help suggests using AXIS(AXIS) to fix the limits, I think the same effect can be acheived with AXIS MANUAL, which would be clearer.
It's also not obvious that the arrow will be a patch object, so has properties like edgecolor, facecolor, rather than line properties.

Jan Motl

Jan Motl (view profile)

Jan Valdman

Jan Valdman (view profile)


Sisi Ma

Sisi Ma (view profile)

I wrote the following code:

hold on;
Z1 = 500;
arrow([max(X) max(Y) Z1],[min(X) min(Y) Z1],15,'BaseAngle',60, 'FaceColor',[0 1 0])

I want to make outputs (which must contain the line along with arrow head) in X-Z plane; i.e. view([180 0]), not in 3D which is good for visualization only. Unfortunately, though the line is visible, the arrow head is not seen when I go into 2D view. I tried to include 'CrossDir',[0 0 1] as suggested by the author of the script, but it didn't help. Any suggestion?

Michael Chan

Michael Chan (view profile)

How do you turn off the arrow heads at either ends of the start and stop points?

Matthew M.

Very helpful and thorough! I only have a couple of minor quibbles - 1) the author didn't make it totally clear what normal MATLAB line properties you should use to change the arrow color and arrowhead color. But it's not too hard to figure out. 2) The author didn't assign message identifiers to the warnings (like for changing the axis limits). I edited the function to add message identifiers so I could turn them off - don't need my Command Window cluttered up with a lot of orange text.

Haochen Tang


Ryan (view profile)

Does anyone know how to add an arrow to the legend? I'm using arrow.m to draw arrows which represent data on the plot and it would be helpful to have a blue arrow in the legend.

Kent Leung

@Matthias, I had this problem too and just stumbled on a solution. (In fact, the problem for me was that the xlabel was disappearing.) To fix this I did:

ax1=subplot(2,1,1); [...]
axes(ax1); arrow([x1 y1],[x2 y2]); arrow fixlimits;

I always do "fixlimits" just in case. The reason I tried this was because in the help file:
"You may want to execute AXIS(AXIS) before calling arrow so it doesn't change the axes on you; arrow determines the sizes of arrow components BEFORE the arrow is plotted, so if arrow changes axis limits, arrows may be malformed."

It's not obvious that this fixed the subplot resizing problem, but it worked!


Felix (view profile)

By far the easiest way to draw arrow compared with arrow3.m and mArrow3.m.

Generally works fine. However, I use subplots, e. g. 3 x 2, and some of them have arrows. Now the arrows change the size of the subplot so that the subplots are no longer of equal size. Any idea how to correct that?


ATN (view profile)

How do I make the arrows dashed, and make the color for each arrow different, corresponding to its z value?

Adding the 'Linestyle',':' for dashed line option makes the arrows look rather strange.

For the colors, I tried creating an nx3 matrix lineColors where each row is a color for the corresponding element, and added the option " 'EdgeColor',lineColors ", but got an error:

"Color value must be a 3 element numeric vector"

Any help is appreciated. Thanks.

Yuanye Wang

Yuanye Wang (view profile)

exactly what I need. thanks a lot.


Tobias (view profile)

Regarding W & Jacopo,

setting the view before the arrow functions solves your problem e.g.:

clc; clear; close all

xvector=[1 0 0];
yvector=[0 1 0];
zvector=[0 0 1];

grid on
daspect([1 1 1])
xlabel('x [mm]', 'FontWeight', 'bold')
ylabel('y [mm]', 'FontWeight', 'bold')
zlabel('z [mm]', 'FontWeight', 'bold')

%Here goes the view setting
view([1 1 1])

arrow([0 0 0],xvector, 'EdgeColor','k','FaceColor','k')
arrow([0 0 0],yvector, 'EdgeColor','b','FaceColor','b')
arrow([0 0 0],zvector, 'EdgeColor','r','FaceColor','r')

%Not here
%view([1 1 1])

Don't no why but seems to work!


John Colby

Wonderful. Thank you, Erik!!!



Jacopo (view profile)

> Christopher This code, at least on >R2009a, looks very strange.
>close all; clc;
>axis([-1 1 -1 1 -1 1])
>I'm really having trouble with 3-D plots >and arrows pointing along the 3rd axis.

I got the same problem on Matlab R2010a (7.10.0)


Will (view profile)

Using arrow with this script yields an unusually long arrow in the z direction, which is quite annoying. I wish the z arrow didn't become long.

clc; clear; close all

xvector=[1 0 0];
yvector=[0 1 0];
zvector=[0 0 1];

grid on
daspect([1 1 1])
xlabel('x [mm]', 'FontWeight', 'bold')
ylabel('y [mm]', 'FontWeight', 'bold')
zlabel('z [mm]', 'FontWeight', 'bold')
arrow([0 0 0],xvector, 'EdgeColor','k','FaceColor','k')
arrow([0 0 0],yvector, 'EdgeColor','b','FaceColor','b')
arrow([0 0 0],zvector, 'EdgeColor','r','FaceColor','r')
view([1 1 1])

Help would be greatly appreciated.

This is great, I messed around with the annotations for 1.5 hours before finding this. Once I found it everything was working the way I wanted it to in a couple of minutes. Great Work!

Marco idiart

Perfect. Just what I was needing.


Zhibo (view profile)

Very nice utility function. I just wonder if it is possible to fix the arrow head size so that it does not change with axis- zoom or resize. Thanks!

Richard Crozier

Really useful, thanks!

Thanks :)


Arthur (view profile)

Very nice, thanks


guizhi (view profile)

can this function specify the 'LineStyle', such as '-' , '--' , '-.' , and ':'?
how to do?
thank you.

thanks a lot. I was trying to make a arrow in a gui window. was having difficulty in correctly saving the arrow location. using your code it works perfectly :)


CC (view profile)

Overall, the script is very useful. However, when using this arrow script, the legend is messed up. Some of the legend symbols would show as "black" squares instead of the correct symbol. When the arrow script is not used, the legend is displayed correctly.

Could anybody please let me know the following:

1. How to change the size of the arrow?
2. Please let me know how to change the type of the arrow head.
3. If possible, please let me know how to have arrow at both the head and tail of the vector.
4. Instead of arrow, please let me know how I can have a dot at both ends of the vector?
5. How to change the line type?


ben harper

can you please include the color options that you write on these comments to the code help lines
thank you



Notan (view profile)

Works great, but the Arrowtips do not look perfect by default as they are the connection of two line ends and therefore not round in pdf printouts.
You can workaround this issue by drawing the arrow in the opposite direction and use parameter: 'End','start'.
But nonetheless it would be nice if they would be perfect by default ;-)


David (view profile)



This code, at least on R2009a, looks very strange.
close all; clc;
axis([-1 1 -1 1 -1 1])

I'm really having trouble with 3-D plots and arrows pointing along the 3rd axis.

Ryan Molecke

I found this implementation to be slow and lacking. Admittedly I was drawing thousands of arrows at extremely short lengths. Here is an inline function I wrote in about 5 minutes to draw simple arrows.

function out = draw_arrow(startpoint,endpoint,headsize)
%by Ryan Molecke

v1 = headsize*(startpoint-endpoint)/2.5;

theta = 22.5*pi/180;
theta1 = -1*22.5*pi/180;
rotMatrix = [cos(theta) -sin(theta) ; sin(theta) cos(theta)];
rotMatrix1 = [cos(theta1) -sin(theta1) ; sin(theta1) cos(theta1)];

v2 = v1*rotMatrix;
v3 = v1*rotMatrix1;
x1 = endpoint;
x2 = x1 + v2;
x3 = x1 + v3;
hold on;
% below line fills the arrowhead (black)
fill([x1(1) x2(1) x3(1)],[x1(2) x2(2) x3(2)],[0 0 0]);
% below line draws line (black)
plot([startpoint(1) endpoint(1)],[startpoint(2) endpoint(2)],'linewidth',2,'color',[0 0 0]);

Rob Campbell

Rob Campbell (view profile)

Useful in many ways but the code is very complicated and it's not clear how to attain specific effects without trawling through the function body. Why not put demo code in a separate file?


Aamir (view profile)


Aamir (view profile)

It looks really great...I'll start use instead of quiver!

never change

amazing,very useful

The arrowheads look distorted. Even the arrowheads in the demo look distorted.


Jianbo (view profile)

Excellent. One little problem is that the arrow created is a little different from the one inserted manually through the "insert" menu. For example, the position can not be adjust by mouse.


Hiram (view profile)

to set the color of the arrow, see this example:
axis([0 3 0 3 0 3]);
grid on
h=arrow([3 3 3],[0 0 0],36,'BaseAngle',60);
set(h, 'FaceColor', 'b');
set(h, 'EdgeColor', 'r');
Awesome application!


Ke (view profile)

very useful.

Saeed Taherion

Leonardo Duarte

lili kalmer

Joselito Dimayuga

the purpose of this Dir or format is for ?????

Dmitry Vasilyev

OMG! Just exactly what I was looking for! Excellent job, thank you Eric!

GDR! x

One can change the colour of an arrow by specifying 'FaceColor' and 'EdgeColor' properties, which is, unfortunately, not documented.

Xuzhang Shen


Alex Fard

Matthijs Klomp

Works great.

Tuan Hoang

sorry, how to add this graphic handle (return value) to an existing axes, figure.

Ahmet Sacan

Does not work when called in a deployed application (i.e., when called from the executable generated using mcc).

Rutty Bee

Ruins the figure when plotting 3-D arrows with z-components. In this case, the width of the arrowhead becomes so large that the plot is rendered unusable. This is the case no matter how one tries to set its size.

Dustin Kruse

works perfectly

Jorge Marin

B Gurt

Does exactly what it needs to do.

jonny e

Excellent and easy to use. I'd emphasize that one should call axis(axis) prior to plotting the arrow, or the axis limits will get reset which will render the arrow incorrectly. This is noted in the help, but tucked towards the bottom.

Ramesh Rajasekaran

Vassili Pastushenko

does not point to (1,1)

Vassili Pastushenko

Good job. A small remark:

arrow([0 0],[1 1],50,10,30)

does not point to (0,1).

Probably it would be reasonable to add a feature of automatical swapping baseAngle and tipAngle, if baseAngle < tipAngle.

Arun Karthi Subramaniyan

Great tool. Very easy to use with detailed documentation. Thanks for sharing it with us.

J Xu

this program is really good, easy-use. 3x Erik.

Nithya C.R.

The program is really great with all the aspects of plotting a directed line excellently covered.

heath kitson

thanks eric


This is great. I hate how MATLAB uses normalized figure coordinates in their annotation() call for drawing arrows. It makes no sense and someone at MathWorks got lazy, so it becomes nearly useless. This code is exactly what they should have done.. good job Erik!

Jasper Menger

Nicely done. A shame by the way Matlab doesn't seem to have a builtin function for drawing arrows from the command line.

Very satisfied

Exactly what I needed. Perfect!

super tanne

Shane Lin

I add the following comment at the file head. arrow(Start,Stop,'EdgeColor','b','FaceColor','b') color arrow.

Vijaykumar Sathyamurthi

Thanks for this excellent tool. I hope you continue writing such winderful codes!!!

Sharoni Shafir

Works great, thanks!

Amit Kesar

Very good. Thank you.

Changshun Deng

Good script!

Dave Cogdell

This code (specifincally including the documentation (which is at the same time incomplete and inadequate) is far too complex for the simple thing(s) it does. I believe the author is an engineer: he should refrain from trying to write like a computer scientist and write like an engineer.

Janik Zikovsky

Very good, better than annotation('arrow') because it is in axis units!

Ian Mackenzie

Evan Westwood

Couldn't figure out how to make the arrow line and arrow head have a specific color. Arrowheads didn't look as nice as those you might plot in drawing/presentation packages such as Powerpoint.

Rattikan Saelim

Nathaniel de Lautour

Superb effort. Highly configurable, easy to use and well documented to boot.

Olivier Marsden

Very useful!

Eddie Fiorelli

Excellent work, thorough documentation. Thanks!

Mattias Forsblom


Kelvin Rocha

Very useful. Explanations are excellent. Exactly what I was looking for.

Gerald Dalley

Very nice

ke wang

Very Useful and convenient. Easy to use!!

Arnd-Ragnar Rhiemeier

Excellent package. Help topics very comprehensive. Easy to use. Quickly leads to appealing graphical output. This package is exactly what I was looking for.

Bernhard Boeck

That's exactly what I'm looking for

Benedikt Halldorsson

Neat package, but (unless I'm missing something obvious) renders itself essentially USELESS.
Try setting the linestyle of the arrow line to dotted (set(h,'linestyle',':') and then try printing it out to a file (print -depsc test.eps) and view the postscript image. The line certainly is not dotted (on Matlab 6.5). Same problem with dashed and dot-dashed lines.
I'm not sure if this is an arrow.m problem, or Matlab's.

Jakob van de Laar

Great package, it should be incorporated in
the standard Matlab distribution

M Bakht

It is just great. Amazing.

Niclas Borlin


Aslak Grinsted

a c

greatest function ever!



Added a 'Color' property (that sets both 'EdgeColor' and 'FaceColor' for arrows drawn with patch objects [the default]) and documentation of how to use it.


Updated for HG2 (R2014b and later)


actually update the file (not just the description); correct view on demo


Replace "eval('trycmd','catchcmd')" with "try, trycmd; catch, catchcmd; end;", which break's compatibility with MATLAB 5 but improves compliance with newer versions.

MATLAB Release
MATLAB 9.0 (R2016a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today