Adding legend to geoglobe with several geoplot3 lines
Show older comments
How can I add a legend to a geoglobe with several geoplot3 lines?
Accepted Answer
More Answers (1)
Steve Schaefer
on 5 Oct 2020
Legend in Geographic Globe - Details on uigridlayout & uipanel Workaround
An example to illustrate the workaround required to add a legend for geoglobe with multiple geoplot3 lines based on uigridlayout created with MATLAB R2020b.
fig1 = uifigure;
uig = uigridlayout(fig1, [1 2]);
uig.ColumnWidth = {'1x',100};
uig.RowHeight = {300,'1x'};
The right column will use a constant width of 100 pixel and the left column will fit to the available space. It is important to set a fixed RowHeight, to avoid resizing of the axes object and the embedded legend. See the uigridlayout documentation for furher information.
Note: GridLayout does not support GeographicGlobe. Create a panel container as a child of the GridLayout using uipanel, and then place the GeographicGlobe in the panel.
uipLeft = uipanel(uig,"BackgroundColor",fig1.Color,"BorderType","none");
uipLeft.Layout.Row = [1 2];
gg = geoglobe(uipLeft,"Terrain","gmted2010");
Plot the data on the geoglobe object
This is the data we use for plotting, assuming we want to use geplot3 on the GeographicGlobe and different altitudes for color coding.
trk = gpxread('sample_mixed','FeatureType','track');
lat = trk.Latitude;
lon = trk.Longitude;
ele = trk.Elevation;
This runs over all altitudes, but could also be any set of lines we want to plot. Use random colors for demo purposes.
targetAltitudes = [3000 4000 5000];
hold(gg,'on')
for tgtAlt = targetAltitudes
geoplot3(gg,lat,lon,tgtAlt, ...
'HeightReference','geoid', ...
'LineWidth',2,'Color',rand(1,3)); %#ok<SAGROW>
end
Create a legend for Geogrpahic Globe:
uipRight = uipanel(uig, 'Title', 'Legend Panel', ...
'BackgroundColor',fig1.Color,'BorderType','none');
uipRight.Layout.Row = 1;
Alternative: The uigridlayout object could be directly used as axes object parent, but without the nice uipanel title. This approach is not shown here.
ax = axes(uipRight,'Position',[0 0 1 1]);
geoLines = get(gg,'Children');
pl = plot(ax,nan(2,numel(geoLines)));
ax.Visible = 'off';
The below line sets the (custom) colors used with geoplot3:
set(pl,{'Color'},get(geoLines,'Color'));
This line creates the actual legend directly below the pseudo legend title given through the uipanel title.
lgd = legend(ax,pl, 'Location','northwest', ...
'Color','none','Box','off');
The below code sets (if required) custom strings for the legend, here the HeightData Property is referenced.
Note: Adjust the numerical value in uig.ColumnWidth if legend strings are too long.
lgd.String = convertStringsToChars(string(...
get(geoLines,'HeightData')));

Categories
Find more on Geographic Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!