Plotting vectors on geoplot
古いコメントを表示
Dear all,
I am trying to add some vector into a geographic coordinate plot. So far I haven't been able to find a way to do this.
Here I attach a simple example demonstrating my problem. I want to have a map with two points and vectors comming out of these 2 points at a 45 degree angle. I can make 2 lines, but I would like to also have arrowheads. I tried using quiver but I cannor really understand how to solve the error it gives.
Thank you for your time, any advise is highly appreciated.
Code follows:
clc; close all; clear all;
coords_A = [39.35, 22.95];
coords_B = [44.65, 10.93];
lats = [coords_A(1); coords_B(1)];
longs = [coords_A(2); coords_B(2)];
figure;
geoscatter(lats, longs, 50, 'b', 'filled');
%Want vector in this direction: (45degrees)
u = 1;
v = 1;
%%
% This works but I really want an arrow tip.
hold on;
geoplot([coords_A(1) coords_A(1)+u],[coords_A(2) coords_A(2)+v],'LineWidth',1,'Color','k');
%%
% This doesnt work
% (Error: Adding Cartesian plot to geoaxes is not supported)
quiver(coords_A(1), coords_A(2), u, v);
%%
% This doesnt work
% (Error: Error using gcm (line 25) Not a map axes).
quiverm(coords_A(1), coords_A(2), u, v);
3 件のコメント
dpb
2019 年 9 月 1 日
Being fully consistent in continuing to introduce duplicated functionality with inconsistent syntaxes and capabilities, it appears TMW has (as of R2018b) introduced another type of axes into the base product geoaxes one of which gscatter creates. But, the doc for it in the Tips sections says explicitly that
- You cannot plot data that requires Cartesian axes in a geographic chart.
so that the ordinary quiver() function won't work is documented and expected behavior. Being new, there apparently was not yet a fully-developed set of extended functions to use the new axes type ready in time for release into the wild.
The Mapping Toolbox uses its own version of a regular axes with particular properties set to be suitable for the purpose of mapping displays...but, being fundamentally a normal axes, you could add to one of the axesm handles.
I don't have Mapping TB to demo/test. I also don't have R2018b installed at the moment so can't see if the annotation object with arrows would work on a geoaxes or not.
"are inconsistent despite both being map axes and as a result quiverm does not work either."
No. That's the problem--they are NOT both map axes; geoscatter() creates and plots into the new "geographic axes" while quiverm() and its compadres that also end with the trailing m plot into the Mapping Toolbox map axis.
But, there is a scatterm() if you do have the TB and it is "just" a regular axes object underneath so you should not be prevented if you use it instead from using quiverm() with it.
回答 (1 件)
david sarria
2020 年 10 月 15 日
編集済み: david sarria
2020 年 10 月 15 日
Hello,
Here is a first attempt of a function to plot an arrow on a geoplot (geographic coordinates plot) from 'begin_point' to 'end_point'. They must be 2D vectors (size 1X2 or 2X1).
begin_point = [begin_latitude begin_longitude]
end_point = [end_latitude end_longitude]
Procedure:
- It plots a line from begin_point to end_point
- to plot the arrow end: plot a line that is begin_point to end_point rotated around end_point by 20 degrees and reduced in length by multiplication by 0.4. Repeat with a -20 degrees angle.
By default, the three parts of the arrow will have different colors. Similar color can be forced using the 'color' option. For example for black:
plot_arrow_geoplot(begin_point,end_point,'color','k')
It can probably be well improved.
-David
function plot_arrow_geoplot(begin_point,end_point,varargin)
if ~(isvector(begin_point) && length(begin_point)==2)
error('begin_point is not a 2D vector')
end
if ~(isvector(end_point) && length(end_point)==2)
error('end_point is not a 2D vector')
end
begin_point = begin_point(:)';
end_point = end_point(:)';
pivot_x = end_point(1);
pivot_y = end_point(2);
theta = 20;
px = begin_point(1);
py = begin_point(2);
s = sind(theta);
c = cosd(theta);
px = (px - pivot_x)*0.4;
py = (py - pivot_y)*0.4;
xnew = px * c - py * s;
ynew = px * s + py * c;
px = xnew + pivot_x;
py = ynew + pivot_y;
theta = -20;
px2 = begin_point(1);
py2 = begin_point(2);
s = sind(theta);
c = cosd(theta);
px2 = (px2 - pivot_x)*0.4;
py2 = (py2 - pivot_y)*0.4;
xnew = px2 * c - py2 * s;
ynew = px2 * s + py2 * c;
px2 = xnew + pivot_x;
py2 = ynew + pivot_y;
% plot
geoplot([begin_point(1) end_point(1)],[begin_point(2) end_point(2)],... % line
[px end_point(1)],[py end_point(2)],... % arrow end first part
[px2 end_point(1)],[py2 end_point(2)],varargin{:}) % arrow end second part
end
カテゴリ
ヘルプ センター および File Exchange で Geographic Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!