Adding legend to geoglobe with several geoplot3 lines

29 views (last 30 days)
How can I add a legend to a geoglobe with several geoplot3 lines?

Accepted Answer

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 17 Sep 2020
Edited: Vinai Datta Thatiparthi on 17 Sep 2020
Hey Julian,
MATLAB at present does not support adding legends to geoglobe. I noticed that this request was already brought to the attention of the concerned parties. They may investigate the matter further.
Presently, as a workaround, consider using the uigridlayout function to create a separate grid, where one grid can be used to display the geoglobe plots and the other grid for displaying the text and legends. In the second grid, you could use the uiaxes function to create an axes and title for the contours and legend. Further, you could use the legend function to create the legends for the geoplots & the drawnow function to ensure that the legend is drawn with the desired colors.
Hope this helps!

More Answers (1)

Steve Schaefer
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];
for tgtAlt = targetAltitudes
geoplot3(gg,lat,lon,tgtAlt, ...
'HeightReference','geoid', ...
'LineWidth',2,'Color',rand(1,3)); %#ok<SAGROW>
Create a legend for Geogrpahic Globe:
Create a dedicated uipanel and use its Title property for a neat formatting.
uipRight = uipanel(uig, 'Title', 'Legend Panel', ...
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.
Place an axes object in the right uipanel, plot dummy data into it and make it not visible.
ax = axes(uipRight,'Position',[0 0 1 1]);
Use the get function to access properties of the lines plotted on the geoglobe object:
geoLines = get(gg,'Children');
pl = plot(ax,nan(2,numel(geoLines)));
ax.Visible = 'off';
The below line sets the (custom) colors used with geoplot3:
This line creates the actual legend directly below the pseudo legend title given through the uipanel title.
lgd = legend(ax,pl, 'Location','northwest', ...
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(...




Community Treasure Hunt

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

Start Hunting!

Translated by