function draw_locs(tbl,latlim,lonlim,nbins,topN,color)
%DRAW_LOCS plots top N locations on a map
                                           
xbinedges = linspace(lonlim(1),lonlim(2),nbins);            % x-axis bin edges     
ybinedges = linspace(latlim(1),latlim(2),nbins);            % y-axis bin edges

[N,~,~] = histcounts2(tbl.X, tbl.Y, xbinedges, ybinedges);  % get bivariate histogram bin counts
sortedN = sort(N(:), 'descend');                            % sort by descending order

for ii = 1:topN                                             % loop over top N
    [i,j] = find(N == sortedN(ii));                         % get the bin indices
    if length(i) == 1                                       % no tie
        loc.lon = [xbinedges(i), xbinedges(i), ...          % x corners of rectangle
            xbinedges(i+1), xbinedges(i+1), xbinedges(i)];
        loc.lat = [ybinedges(j), ybinedges(j+1), ...        % y corners of rectangle
            ybinedges(j+1), ybinedges(j), ybinedges(j)];
        linem(loc.lat, loc.lon, 'LineStyle', '-', ...       % draw line
            'Color', color)
        if ii == 1                                          % if top 1
            textm(loc.lat(1), loc.lon(1), '#1', ...         % add annotation
                'Color', color)
        end
    else                                                    % we have ties
        for k = 1:length(i)                                 % iterate over ties
            loc.lon = [xbinedges(i(k)), xbinedges(i(k)), ...% x corners of rectangle
                xbinedges(i(k)+1), xbinedges(i(k)+1), xbinedges(i(k))];
            loc.lat = [ybinedges(j(k)), ybinedges(j(k)+1),... % y corners of rectangle
                ybinedges(j(k)+1), ybinedges(j(k)), ybinedges(j(k))];
            linem(loc.lat, loc.lon, 'LineStyle', '-', ...   % draw line
                'Color', color)
        end
    end
end

end

