def find_contours_level(density, x, y, level, closed=False):
    """Find iso-valued density contours for a given level value

    Parameters
    ----------
    density: 2d ndarray of shape (M, N)
        Kernel density estimate for which to compute the contours
    x: 2d ndarray of shape (M, N) or 1d ndarray of size M
        X-values corresponding to `kde`
    y: 2d ndarray of shape (M, N) or 1d ndarray of size M
        Y-values corresponding to `kde`
    level: float between 0 and 1
        Value along which to find contours in `kde` relative
        to its maximum kde

    Returns
    -------
    contours: list of ndarrays of shape (P, 2)
        Contours found for the given level value

    See Also
    --------
    skimage.measure.find_contours: Contour finding algorithm used
    """
    if level >= 1 or level <= 0:
        raise ValueError("`level` must be in (0,1), got '{}'!".format(level))
    # level relative to maximum
    level = level * density.max()
    # xy coordinates
    if len(x.shape) == 2:
        assert np.all(x[:, 0] == x[:, 1])
        x = x[:, 0]
    if len(y.shape) == 2:
        assert np.all(y[0, :] == y[1, :])
        y = y[0, :]
    if closed:
        # find closed contours
        density = np.pad(density, ((1, 1), (1, 1)), mode="constant")
        offset = 1
    else:
        # leave contours open at kde boundary
        offset = 0

    conts_idx = find_contours(density, level)
    conts_xy = []

    for cc in conts_idx:
        cx = np.interp(x=cc[:, 0]-offset,
                       xp=range(x.size),
                       fp=x)
        cy = np.interp(x=cc[:, 1]-offset,
                       xp=range(y.size),
                       fp=y)
        conts_xy.append(np.stack((cx, cy), axis=1))

    return conts_xy
def find_contours_level ( density , x , y , level , closed = False ) : if level >= 1 or level <= 0 : raise ValueError ( "`level` must be in (0,1), got '{}'!" . format ( level ) ) level = level * density . max ( ) if len ( x . shape ) == 2 : assert np . all ( x [ : , 0 ] == x [ : , 1 ] ) x = x [ : , 0 ] if len ( y . shape ) == 2 : assert np . all ( y [ 0 , : ] == y [ 1 , : ] ) y = y [ 0 , : ] if closed : density = np . pad ( density , ( ( 1 , 1 ) , ( 1 , 1 ) ) , mode = "constant" ) offset = 1 else : offset = 0 conts_idx = find_contours ( density , level ) conts_xy = [ ] for cc in conts_idx : cx = np . interp ( x = cc [ : , 0 ] - offset , xp = range ( x . size ) , fp = x ) cy = np . interp ( x = cc [ : , 1 ] - offset , xp = range ( y . size ) , fp = y ) conts_xy . append ( np . stack ( ( cx , cy ) , axis = 1 ) ) return conts_xy
Find iso-valued density contours for a given level value

    Parameters
    ----------
    density: 2d ndarray of shape (M, N)
        Kernel density estimate for which to compute the contours
    x: 2d ndarray of shape (M, N) or 1d ndarray of size M
        X-values corresponding to `kde`
    y: 2d ndarray of shape (M, N) or 1d ndarray of size M
        Y-values corresponding to `kde`
    level: float between 0 and 1
        Value along which to find contours in `kde` relative
        to its maximum kde

    Returns
    -------
    contours: list of ndarrays of shape (P, 2)
        Contours found for the given level value

    See Also
    --------
    skimage.measure.find_contours: Contour finding algorithm used