フィルターのクリア

Can I plot a digraph on a geomap?

22 ビュー (過去 30 日間)
Trevor John D Silva
Trevor John D Silva 2021 年 11 月 11 日
コメント済み: Chris Hooper 2024 年 7 月 3 日 20:19
Hey there!
Similar to the question asked here (https://www.mathworks.com/matlabcentral/answers/472642-undirected-graph-network-with-latitude-and-longitude-as-node), I want to know if there is a way I could apply a digraph on geomap.
I couldn't find a way to create nodes with coordinate data (lat & lon), and thus gave arbitary ids (1,2,3...) for the nodes instead. After creating an array of targets (with similar id information), I make an arbitary array of weights and then plot it the graph.
G = digraph(s,t,w,);
plot(G, 'XData',x,'YData',y)
Because the problem is originally a geo problem, my x and y data is in the form of coordinates. However, is there a way to connect this information to GeoAxes so that I can tell Matlab to "accurately" overlay the digraph over the geomap?
A (probably messy) of doing it is to use an image as a background - which (I'm guessing) won't be reliable when reszing the plot.
--
PS: I've read the blog post (https://www.mathworks.com/matlabcentral/answers/472642-undirected-graph-network-with-latitude-and-longitude-as-node), while helpful for sure, Loren uses polygons to create the map of the US, which is overkill for what I want to achieve.
PPS: If you need some more info, please let me know in case I must've missed out anything.
  2 件のコメント
Michael Wish
Michael Wish 2022 年 1 月 17 日
I wish someone would answer this! I need the exact same thing!
Christofer Pankratz
Christofer Pankratz 2023 年 9 月 6 日
編集済み: Christofer Pankratz 2023 年 9 月 6 日
+1
while it's not an ideal solution, but could work for your purpose as well

サインインしてコメントする。

回答 (1 件)

Avni Agrawal
Avni Agrawal 2024 年 2 月 15 日
I understand that you are trying to plot a diagraph on a geomap. Yes, you can overlay a directed graph (`digraph`) on a geographic map (`geoaxes`) in MATLAB. To do this, you would use the latitude and longitude as the `XData` and `YData` properties when plotting the graph. However, since `geoaxes` is for geographic plotting, you actually need to use the latitude for `YData` and longitude for `XData` because latitude corresponds to the Y-axis (north-south) and longitude corresponds to the X-axis (east-west) on a map.
Here is how you can create a `digraph` and plot it on a geographic map:
% Define the node IDs and their corresponding latitude and longitude
nodeIDs = [1, 2, 3, ...]; % Replace with your node IDs
lats = [lat1, lat2, lat3, ...]; % Replace with your node latitudes
lons = [lon1, lon2, lon3, ...]; % Replace with your node longitudes
% Define the edges between nodes as source and target pairs
s = [sourceNodeIDs]; % Replace with your source node IDs
t = [targetNodeIDs]; % Replace with your target node IDs
% Define the weights for the edges
w = [weights]; % Replace with your edge weights
% Create the directed graph
G = digraph(s, t, w);
% Create a geographic axes object
figure;
geoAx = geoaxes;
% Plot the graph on the geographic axes
% Note: The 'XData' and 'YData' inputs to 'plot' should correspond to longitude and latitude
hold(geoAx, 'on'); % Hold on to plot multiple items on the geoaxes
plot(geoAx, G, 'XData', lons(G.Edges.EndNodes), 'YData', lats(G.Edges.EndNodes), 'LineWidth', 2);
% Optionally, you can customize the display, such as setting limits or adding a basemap
geobasemap(geoAx, 'streets'); % Set the basemap to 'streets'
geolimits(geoAx, [min(lats) max(lats)], [min(lons) max(lons)]); % Set geographic limits
% Release the hold on the geoaxes
hold(geoAx, 'off');
Make sure to replace `lat1`, `lat2`, `lat3`, `lon1`, `lon2`, `lon3`, `sourceNodeIDs`, `targetNodeIDs`, and `weights` with the actual values you have for your nodes and edges.
By using `geoaxes` and `geobasemap`, you can take advantage of MATLAB's built-in geographic plotting capabilities, which will handle things like map projection and resizing automatically. This approach will give you a more accurate and interactive map icompared to using a static image as a background.
I hope this helps!
  2 件のコメント
Francesco
Francesco 2024 年 4 月 15 日
Thanks for your answer.
I have a problem plotting the digraph on geoaxes. When I run the plot row, It return me an error like this:
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in congestion_research (line 140)
H = plot(geoAx,G,'XData',xdata,'YData',ydata,'LineWidth',LWidths);
In my case, geoAx is simply geoAx = geoaxes; G is a digraph, xdata and ydata are duble vectors with Cartesian Longitudes and Latitudes for each nodes and also LWidths is a double.
Where can I search for the error?
Thanks in advance
Francesco
Chris Hooper
Chris Hooper 2024 年 7 月 3 日 20:19
I am also having this same issue.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeographic Plots についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by