Creating a map using x/y data

37 views (last 30 days)
Louise Wilson
Louise Wilson on 4 Apr 2022
Answered: Harshit on 15 Sep 2023
I have X, Y, Z data where X and Y are lon and lat coordinates and Z is value of interest. All variables are 12500*1800 (too large to attach here).
I would like to plot these variables on map axes so I can add shape files with additional info., aswell as a scale bar.
clf
axesm('MapProjection','mercator') %define axis
surfm(y,x,z) % plot X/Y data
S = shaperead('study_site.shp','UseGeoCoords',true); %add shapefile on top of X/Y data
info = shapeinfo('study_site.shp');
crs = info.CoordinateReferenceSystem
geoshow(S,'FaceColor','white');
Apart from needing to adjust the axes limits, this looks good. But, I am struggling to add a scale bar. When I try to do this, the plot is just replaced with a scale bar:
hold on
scaleruler on
How do I do this properly?
Also, how do I add lat and lon information to the frame?

Answers (1)

Harshit
Harshit on 15 Sep 2023
Hi,
I understand that you want to plot the latitudes and longitudes onto the map axes.
For such large variables of "X", "Y", and "Z", I suggest creating a meshgrid of lon and lat coordinates first. Then, you can use the "pcolorm" function to plot the "Z" variable on the map.
Additionally, you use the "scaleruler" function to add a scale bar to the frame.
Here's an updated version of your code incorporating the above suggestions:
clf
axesm('MapProjection', 'mercator')
% Create a meshgrid of lon and lat coordinates
[lon_grid, lat_grid] = meshgrid(x, y);
% Plot the Z variable using pcolorm
pcolorm(lat_grid, lon_grid, z);
S = shaperead('study_site.shp', 'UseGeoCoords', true);
info = shapeinfo('study_site.shp');
crs = info.CoordinateReferenceSystem;
geoshow(S, 'FaceColor', 'white');
% Adjust axes limits if needed
% xlim([lon_min, lon_max]);
% ylim([lat_min, lat_max]);
% Add scale bar
scaleruler('RulerStyle', 'patches', 'Units', 'km', 'FontSize', 10);
% Add latitude and longitude information to the frame
framem on;
gridm on;
mlabel on;
plabel on;
To find more about the functions used here, kindly refer to the following documentation pages:
Hope the above information resolves your issue.
Regards,
Harshit

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!