def grid_communities(c):
    '''
    (X,Y,INDSORT) = GRID_COMMUNITIES(C) takes a vector of community
    assignments C and returns three output arguments for visualizing the
    communities. The third is INDSORT, which is an ordering of the vertices
    so that nodes with the same community assignment are next to one
    another. The first two arguments are vectors that, when overlaid on the
    adjacency matrix using the PLOT function, highlight the communities.

    Parameters
    ----------
    c : Nx1 np.ndarray
        community assignments

    Returns
    -------
    bounds : list
        list containing the communities
    indsort : np.ndarray
        indices

    Notes
    -----
    Note: This function returns considerably different values than in
    matlab due to differences between matplotlib and matlab.  This function
    has been designed to work with matplotlib, as in the following example:

    ci,_=modularity_und(adj)
    bounds,ixes=grid_communities(ci)
    pylab.imshow(adj[np.ix_(ixes,ixes)],interpolation='none',cmap='BuGn')
    for b in bounds:
      pylab.axvline(x=b,color='red')
      pylab.axhline(y=b,color='red')

    Note that I adapted the idea from the matlab function of the same name,
    and have not tested the functionality extensively.
    '''
    c = c.copy()
    nr_c = np.max(c)
    ixes = np.argsort(c)
    c = c[ixes]

    bounds = []

    for i in range(nr_c):
        ind = np.where(c == i + 1)
        if np.size(ind):
            mn = np.min(ind) - .5
            mx = np.max(ind) + .5
            bounds.extend([mn, mx])

    bounds = np.unique(bounds)
    return bounds, ixes