I would like to add arrows to a plot of a line in Matlab to illustrate flow. The arrows would ideally be on the line pointing in the direction of the line. Is this possible?
-
1possible duplicate of [How do I display an arrow positioned at a specific angle in MATLAB?](http://stackoverflow.com/questions/1803043/how-do-i-display-an-arrow-positioned-at-a-specific-angle-in-matlab) – gnovice Sep 30 '10 at 15:35
5 Answers
In order to draw an arrow in Matlab, use the file exchange free package called arrow.m

- 20,795
- 11
- 69
- 104
Another way is to use great submission on FileExchange - ARROW.M
See also other related questions on SO:
The quiver function should be able to do what you want. However, you'll have to compute the direction of the arrow yourself.
Something along the lines of this is ugly but should get you started (but you probably want to normalize the direction vector to get a nicer graph)
plot(x,y)
hold on
quiver(x(1:end-1), y(1:end-1), ones(len(x)-1,1), y(2:end) - y(1:end-1))

- 6,891
- 5
- 35
- 46
If I understood correctly, you are trying to view a vector field? If that's the case, here is a working example:
%# function: f(x,y)=x^3-2y^2-3x over x=[-2,2], y=[-1,1]
[X Y] = meshgrid(-2:.1:2, -1:.1:1);
Z = X.^3 -2*Y.^2 -3*X;
%# gradient of f
[dX dY] = gradient(Z, .1, .1);
%# plot the vector field and contour levels
figure, hold on
quiver(X, Y, dX, dY)
contour(X, Y, Z, 10)
axis equal, axis([-2 2 -1 1])
hold off
%# plot surface
figure, surfc(X, Y, Z)
view(3)

- 123,847
- 25
- 243
- 454
You can add an arrow to a figure by using the function annotation
.
Note that the parent of the object is not the axes, but the figure window iteslf (meaning that when you zoom in/out, the arrow keeps on the same location in the figure window).

- 137
- 9