How can I overlay vector arrows to an image of a 2D matrix?
조회 수: 53 (최근 30일)
이전 댓글 표시
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
Jan
2022년 2월 8일
Please mention, what "didn't work" mean. Do you get an error message or does the result differ from your expectations?
답변 (3개)
DGM
2022년 2월 8일
편집: DGM
2022년 2월 8일
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.
댓글 수: 0
William Rose
2022년 2월 8일
편집: William Rose
2022년 2월 8일
[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
Shai Katz
2022년 6월 26일
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!
William Rose
2022년 6월 27일
@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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Geographic Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!