How to add a legend in the Mapping Toolbox?

12 views (last 30 days)
I'm trying to add a legend for serveral values that I plotted on a map using scatterm().
I simply want to add a legend and I'm having a hard time finding it in the Mapping Toolbox and online in MatLab answers. I want the blue, red, and grey values to be legend('P-Value < 5%', ' 5% < P-Value < 15%' , 'P-Value > 15%') correspondingly. Normal legend() doesn't work.
Here is the code:
latlim = [25 50]; lonlim = [-125 -67];
axe = usamap(latlim, lonlim);
set(axe, 'Visible', 'off');
sheds = shaperead('usastatehi','UseGeoCoords', true, 'BoundingBox', [lonlim', latlim']);
geoshow(axe, sheds, 'FaceColor', [1 1 1])
scatterm(latb, longb, 12, blueVal, 'filled', 'blue')
hold on
scatterm(latr, longr, 12, redVal, 'filled', 'red')
scatterm(latg, longg, 12, [0.7 0.7 0.7])
  1 Comment
Adam Danz
Adam Danz on 21 Aug 2019
" Normal legend() doesn't work. "
If you have any trouble implementing the answer (that uses a normal legend), please let us know what the prblems are.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 19 Aug 2019
Edited: Adam Danz on 20 Aug 2019
Use the DisplayName property (you can change the labels from my example)
Use the output handles produced in scatterm()
Provide those handles to legend().
h(1) = scatterm(latb, longb, 12, blueVal, 'filled', 'blue','DisplayName', 'p < 5%');
hold on
h(2) = scatterm(latr, longr, 12, redVal, 'filled', 'red','DisplayName', '5% < p < 15%');
h(3) = scatterm(latg, longg, 12, [0.7 0.7 0.7], 'DisplayName', 'p > 15%');
legend(h)
190820 093531-Figure 1.jpg

More Answers (0)

Community Treasure Hunt

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

Start Hunting!