Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add arrows to line plots in Matlab?

Tags:

plot

matlab

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?

like image 239
Reed Richards Avatar asked Sep 30 '10 12:09

Reed Richards


4 Answers

In order to draw an arrow in Matlab, use the file exchange free package called arrow.m

like image 157
Andrey Rubshtein Avatar answered Oct 12 '22 02:10

Andrey Rubshtein


Another way is to use great submission on FileExchange - ARROW.M

See also other related questions on SO:

like image 31
yuk Avatar answered Oct 12 '22 02:10

yuk


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))
like image 36
Kena Avatar answered Oct 12 '22 01:10

Kena


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)

vector fieldsaddle surface

like image 40
Amro Avatar answered Oct 12 '22 02:10

Amro