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

39 views (last 30 days)
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 Comments
Yves
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;
[dX,dY]=gradient(Z);
qx=-dX;
qy=-dY;
quiver(dX,dY,qx,qy,'k');

Sign in to comment.

Answers (3)

DGM
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);
[px,py] = gradient(z,.2,.15);
% 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
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
I = imread('mtEverest.jpg') ;
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);
%add arrows to plot
quiver(x,y,u,v,'-k')
Try it.
  1 Comment
Shai Katz
Shai Katz on 26 Jun 2022
Hi,
I also tried to use quiver an plot it above a geographical map from geobasemap - and I always get an error that:
"Error using newplot (line 86)
Adding Cartesian plot to geoaxes is not supported."
I tried to use local2latlon function to get the geographcical cordiantes for lat and lon, but then I need to make a new caculation for u and v with geographical cordinates - How do I do this I caculate with geographical cordinates?
P.s using quiverm isn't working good..
Thanks!

Sign in to comment.


William Rose
William Rose on 27 Jun 2022
@Shai Katz, the quiver command automatically scales the lengths of the arrows so that they do not overlap. Therefore you do not need to assign lat & long to the u,v, values. You can override the automatic scaling if you want - see the man page for quiver for details.
Perhaps when you If you are really intent on trying to assign lat, long values to u,v, I suggest rescaling your existing u,v so that after rescaling, the longest arrow's length, sqrt(u^2+v^2), is 5% of the latitude or longitude span of your image. The see how it looks and adjust as you see fit.
The error you repoted, about geoaxes being incompatible with Cartesian plots, makes me worry that you will never get quiver to work with a geo plot. But maybe you have found a way. In the example I gave above, I just used an image, which IS compatible with a Cartesian plot.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by