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?
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;

Sign in to comment.

Answers (2)

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():
% 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
%add arrows to plot
Try it.

Community Treasure Hunt

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

Start Hunting!