How to draw good looking arrows in Matlab?

17.7k views Asked by At

I have been trying to draw arrows in Matlab in my figure but they all look terrible. Also, I want my arrowhead to be a solid triangle, not a V.

I tried using the packages available on the Matlab File Exchange: arrow, arrows, arrows3, and probably at least one other one.

I even tried manually creating an arrow in the Matlab figure editor, but when I adjust the line width, the arrow looks like this:

enter image description here

I used the annotation command to create the arrow above:

annotation(gcf,'arrow',[0.621875 0.457916666666667],...
                       [0.205421152030217 0.40755429650614],...
                       'HeadLength',4,'LineWidth',5);

Here's the result trying to use the arrow package available here: Arrow.m (notice how the bottom arrow head is not perpendicular to the line:

enter image description here

I even tried the following and here is the result below (notice the terrible looking arrowhead):

 figure
 plot(1:10, 1:10)
 annotation(gcf,'arrow',[0.621875 0.457916666666667],...
                        [0.205421152030217 0.40755429650614],...
                        'HeadLength',4,'LineWidth',5);

enter image description here

7

There are 7 answers

0
Veridian On BEST ANSWER

I resorted to installing InkScape and drawing arrows in there. Nothing beat the simplicity and quality.

InkScape Website

4
bla On

Another solution is to use the open-source Waterloo graphics - a library that addresses this by providing a pure Java library of 2D graphics functions that can easily be integrated in Matlab. See some examples here...

For example, try this code (after properly installing waterloo)

f = GXFigure();
x = -5:0.1:5;
gr1 = gxgca();
a1 = line(gr1, x, cos(x), 'LineSpec','-ob');
b1 = line(gr1, x, sin(x), 'LineSpec','-sg');
annotation(gr1,'arrow',[0.1 0.1],[0.4 0.4],'HeadLength',0.2,'HeadWidth', 0.5, 'LineWidth',2);
gr1.getObject().getView().autoScale();

enter image description here

8
Bas Swinckels On

Having worked with Matlab for more than 10 years and seeing almost zero progress in the quality of the plots (anti-aliased output to bitmap, decent looking eps-files, ...), I decided that my long time solution will be here. Some examples of decent looking arrows here, more beautiful graphs here. Unfortunately, some of the toolboxes prevent me from going completely to numpy/scipy/matplotlib. I know this is more of a rant than an answer, but that is my solution ...

5
horchler On

Vector graphics is hard. Though Matlab's typography is just as bad, but here's a simplistic text-based solution (I refuse to do this sort of annotation in Matlab any more):

figure
plot(1:10, 1:10)
text(5,4,'\rightarrow','FontSize',54,'Rotation',135,...
         'HorizontalAlignment','center');

which yields a figure like this

                        enter image description here

Note that I have used '\leftarrow' because it points in the direction of zero degrees, which makes doing math in my head easier. This is no canned solution, you'll still need to fiddle with position to overcome the fact that Matlab is aligning this as text (see the 'Extent' and 'Margin' properties). Not surprisingly, you may see small glitches. The LaTeX interpreter can be used to obtain a different style arrow head:

    text(5,4,'$\rightarrow$','FontSize',54,'Rotation',135,...
             'HorizontalAlignment','center','Interpreter','latex');

I don't get the small glitches with this option, but the arrows look different (there are likely other LaTeX arrow styles that could be substituted). Changing the font may also have an effect and there are certainly other text-based arrows that could be used. More details on adding arrows can be found in this article from The MathWorks.

10
gnovice On

When line-based arrows fail, you can always try patch-based ones:

xArrow = [0 1 1 1.2 1 1 0];               % X-coords of arrow edge points
yArrow = [-0.1 -0.1 -0.2 0 0.2 0.1 0.1];  % Y-coords of arrow edge points
hArrow = fill(xArrow,yArrow, [1 0 0]);    % Plot a red arrow
axis equal                                % Set axis tick marks to be equal

enter image description here

Although more work, you can potentially parameterize the above into a function that takes a number of arguments for adjusting the scaling, aspect ratio, rotation, and position of the arrow. You can even adjust the color, edge line color, and alpha transparency.

This sort of idea appears to have been implemented by François Beauducel in his File Exchange submission ARROWS: generalized 2-D arrows plot.

3
dynamo On

Using quiver or quiver3 for 3-D plots will certainly look better than the that last arrow you made using annotation. I'm not sure it will look much better than the ones you made using the Arrow.m package though. It's possible to change the stlyes on quiver as well if you want.

0
Leonard Wayne On

There is now the DaVinci Draw toolbox (full disclosure: I wrote/sell the 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.Width', 1,           ...
                  'Head.Length', 2.5,         ...
                  'Color',       'w',         ...
                  'EdgeColor',   'k',         ...
                  'LineWidth',   2.5 )

From the documentation:

DaVinci arrow