# How can I overlay vector arrows to an image of a 2D matrix?

23 views (last 30 days)
Yves on 8 Feb 2022
Edited: William Rose on 8 Feb 2022
I obtained through the imagesc function, the figure relating to 2D wind direction matrix, but now to improve the representation I need to add, the vector field above.I try quiver function, but it didn't work. How can I do it?
##### 2 CommentsShowHide 1 older comment
Yves on 8 Feb 2022
I get an error. I wrote this
imagesc(windDirection);shading flat; colormap parula; colorbar; set(gca, 'YDir','reverse');axis xy; axis square tight; hold on;
Z=windDirection;
qx=-dX;
qy=-dY;
quiver(dX,dY,qx,qy,'k');

DGM on 8 Feb 2022
Edited: DGM on 8 Feb 2022
You're not specifying any scale information when calling imagesc(), so its scale is implicitly in pixels. Depending on what your vector data looks like, one or the other will probably be tiny compared to the other or there will be some offset.
Like this:
% some example data
[x,y] = meshgrid(-2:.2:2,-1:.15:1);
z = x .* exp(-x.^2 - y.^2);
% no scale information provided
imagesc(z), hold on
hq = quiver(x,y,px,py);
hq.Color = 'k';
axis image The solution is to provide the x and y information to imagesc():
clf
% use x,y data to define the scale & location of the image
imagesc(x(:),y(:),z), hold on
hq = quiver(x,y,px,py);
hq.Color = 'k';
axis image Note that imagesc() only needs the minimum and maximum x and y values, but if you feed it a larger vector, it'll find the min and max internally.

William Rose on 8 Feb 2022
Edited: William Rose on 8 Feb 2022
[editing my answer: I used image() before, but you wanted imagesc(), so now my code uses imagesc()]
I agree with @Jan. I have had good results with quiver and quiver3. If you include your code, or a simpliified version of it that captures the key aspcts of the problem, and attach an image file, it will help others help you.
Perhaps the problem with quiver was that you have plotted an image and you need to know how to add a numeric plot on top of that. Here is an example of the wind field for a cyclone centered over a contour map of Mt. Everest:
%get image and plot it, with a coordinate system defined undeneath it
imagesc([-4 4], [-4 4],I); % show the image
hold on
%make data for quiver plot
r=[1,2,3];
theta=(0:7)*pi/4;
x=r'*cos(theta);
y=r'*sin(theta);
u=(1./r')*sin(theta);
v=-(1./r')*cos(theta);
quiver(x,y,u,v,'-k') Try it.