I need help adjusting the display axes values of a map created in matlab.

44 views (last 30 days)
Hello,
I am trying to adjust the axis properties of a map I created using the mapping tool box. I was able to figure how to segment my image based on a lat/long interval. However, I am not sure how to adjust the significant figures displayed. Secondly, I want to rotate the displayed horizontal axis values. I realized regular matlab xticklabelrotation does not work in this case. I have not found a way to do so looking at the properties of getm(gca). Thank you for your time.
I have copied and pasted a snippet of my code and the image that was produced.
map_intv=abs(min([(Lat_high-Lat_low)/4 (Long_high-Long_low)/4]));
figure
ax = usamap(latlim, lonlim)
getm(gca,'MapProjection')
geoshow(ax,Nodalarray(:,28), Nodalarray(:,29), 'DisplayType', 'Point', 'Marker', '.', 'Color', 'red');
xtickangle(45)
setm(gca,'MeridianLabel','on','ParallelLabel','on','MLineLocation',map_intv,'PLineLocation',map_intv,'MLabelLocation',map_intv,'PLabelLocation',map_intv)
The result I am looking for is something similar to the following ( I edited the image via MSpaint ). The label rotation may be 90 or other specified angle such as 45. What would be more important is the specification of the significant figures so the labels are now meaningful.

Accepted Answer

Adam Danz
Adam Danz on 29 Jun 2020
Edited: Adam Danz on 29 Jun 2020
The difference between typical axis ticks and map axis ticks.
The latitude and longitude labels in a map are pseudo-tick-labels. If you look at the x and y tick properties of the map axis handle, they are empty. The tick labels in a map are actually text objects that are children of the map axes. Furthermore, each label actually consists of two labels and the first one is empty. This places space between the labels and the axes.
Method 1: rotate the text objects
The idea is to get the handles to the pseudo ticks, remove the empty portion of the labels, and rotate the text objects.
Create demo map
clf()
lon = -linspace(97.40, 97.45,5);
lat = linspace(35.70, 35.75, 5);
ax = usamap([min(lat),max(lat)], [min(lon),max(lon)]);
setm(gca,'MeridianLabel','on','ParallelLabel','on','MLineLocation',lon,'PLineLocation',lat,'MLabelLocation',lon,'PLabelLocation',lat)
Get handles to the pseudo x tick labels
xticklabelHandles = findall(gcf,'Type','text','Tag','MLabel');
nLabeles = numel(xticklabelHandles);
% For the pseudo y-tick labels,
% xticklabelHandles = findall(gcf,'Type','text','Tag','PLabel');
Plot the anchor position for each pseudo x tick label
This will be useful to verify that labels are correctly rotated. The markers can be removed later.
xtickPosition = cell2mat(get(xticklabelHandles,'Position'));
hold(ax,'on')
anchors = plot(ax,xtickPosition(:,1), xtickPosition(:,2), 'rx');
Remove the first, empty row from each label
Notice the tick labels shift upward.
xTickLabels = get(xticklabelHandles, 'String');
xTickLabels = cellfun(@(c)c(2),xTickLabels);
set(xticklabelHandles, {'String'}, xTickLabels)
Rotate the labels 90 degrees
% Rotate text
set(xticklabelHandles,'Rotation', 90)
Notice how the labels were rotated about the anchor points. To fix this we must adjust the vertical and horizontal alignment properties in order to reposition the labes relative to the anchors.
Adjust the HorizontalAlignment and VerticalAlignment
To place the labels under the anchor, set their horizontal alignment to right so the marker is just above the "W" in the lables and to set the vertical alignment to middle so the labels are centered from left to right in the extent boxes.
set(xticklabelHandles, 'HorizontalAlignment', 'Right', 'VerticalAlignment', 'Middle')
But if you try that, the labels aren't centered under the anchors. Their vertical aligment is off.
Shift labels so they are centered and away from the axes
To add some space between the labels and axes, after setting the horizontal and vertical alignment properties, I shifted the labels downward by 3% of the y axis range. You may need to adjust the amount of shifts to suit your needs. I also needed to shift the axes upward and decrease the axis height to fit the labels on the axes
% Shift labels downward
xtickPosition(:,2) = xtickPosition(:,2) - range(ax.YLim)*.03;
set(xticklabelHandles, {'Position'}, mat2cell(xtickPosition, ones(nLabeles,1),3))
% shift axes upward
ax.Position([2,4]) = ax.Position([2,4]) .* [1.5, 0.9];
% Remove anchors markers
delete(anchors)
Method 2: Remove the text objects and set real x tick labels
Pros: you're dealing with real xtick labels and no longer have to worry about axis position etc.
Con: you have to turn the axes on.
Create demo map
clf()
lon = -linspace(97.40, 97.45,5);
lat = linspace(35.70, 35.75, 5);
ax = usamap([min(lat),max(lat)], [min(lon),max(lon)]);
setm(gca,'MeridianLabel','on','ParallelLabel','on','MLineLocation',lon,'PLineLocation',lat,'MLabelLocation',lon,'PLabelLocation',lat)
Get handles to the pseudo x tick labels
xticklabelHandles = findall(gcf,'Type','text','Tag','MLabel');
nLabeles = numel(xticklabelHandles);
% For the pseudo y-tick labels,
% xticklabelHandles = findall(gcf,'Type','text','Tag','PLabel');
Store the pseudo tick position and labels
xtickPosition = cell2mat(get(xticklabelHandles,'Position'));
xTickLabels = get(xticklabelHandles, 'String');
xTickLabels = cellfun(@(c)c(2),xTickLabels);
Delete the text labels
delete(xticklabelHandles)
Set the real xtick and xticklabel and turn the axes on
Notice that the background axes that were once not visible are now visible.
set(ax,'XTick',xtickPosition(:,1), 'XTickLabel', xTickLabels)
axis on
Rotate the xtick labels
ax.XTickLabelRotation = 90
  2 Comments

Sign in to comment.

More Answers (1)

Trevor
Trevor on 7 Jul 2022
Amazing anwser, thanks so much!

Community Treasure Hunt

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

Start Hunting!

Translated by