MATLAB: How to add a legend in the Mapping Toolbox

legendmappingscatterm

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])

Best Answer

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
Related Question