Beispiel #1
0
def scatter_density(x,
                    y,
                    res=100,
                    cmap=matplotlib.cm.hot_r,
                    size=None,
                    return_cbar=False,
                    **kwargs):
    """
    Create a scatter plot with density of the data-points at each point on the
    x,y grid coded by the color in the colormap (hot per default)

    **kwargs are passed to matplotlib's matshow
    
    """

    x = np.copy(x)
    y = np.copy(y)

    max_x = np.nanmax(x)
    max_y = np.nanmax(y)
    min_x = np.nanmin(x)
    min_y = np.nanmin(y)

    x = ozu.rescale(x).ravel() * (res - 1)
    y = ozu.rescale(y).ravel() * (res - 1)

    data_arr = np.zeros((res, res))

    for this_x, this_y in zip(x, y):
        # If one of them is a nan, move on:
        if np.isnan(this_x) or np.isnan(this_y):
            pass
        else:
            data_arr[int(np.floor(this_x)), int(np.floor(this_y))] += 1

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    imax = ax.matshow(np.log10(np.flipud(data_arr.T)), cmap=cmap, **kwargs)
    cbar = fig.colorbar(imax)
    ax.set_xticks([0] + [i * res / 5.0 for i in range(5)])
    ax.set_yticks([0] + [i * res / 5.0 for i in range(5)])
    ax.set_xticklabels(
        [0] +
        ['%0.2f' % (i * ((max_x - min_x) / 5.0) + min_x) for i in range(5)])
    ax.set_yticklabels([0] + [
        '%0.2f' % (i * ((max_y - min_y) / 5.0) + min_y)
        for i in range(5, 0, -1)
    ])

    if size is not None:
        fig.set_size_inches(size)
    if return_cbar:
        return fig, cbar
    else:
        return fig
Beispiel #2
0
def test_rescale():
    """
    Test rescaling of data into [0,1]
    """

    data = np.random.randn(20) * 100
    rs = ozu.rescale(data)
    npt.assert_equal(np.max(rs), 1)
    npt.assert_equal(np.min(rs), 0)

    # Test for conditions in which the minimum is >0:
    data = (np.random.rand(20) + 10) * 100
    rs = ozu.rescale(data)
    npt.assert_equal(np.max(rs), 1)
    npt.assert_equal(np.min(rs), 0)
Beispiel #3
0
def test_rescale():
    """
    Test rescaling of data into [0,1]
    """

    data = np.random.randn(20) * 100
    rs = ozu.rescale(data)
    npt.assert_equal(np.max(rs),1)
    npt.assert_equal(np.min(rs),0)

    # Test for conditions in which the minimum is >0:
    data = (np.random.rand(20) + 10) * 100
    rs = ozu.rescale(data)
    npt.assert_equal(np.max(rs),1)
    npt.assert_equal(np.min(rs),0)
Beispiel #4
0
def scatter_density(x,y, res=100, cmap=matplotlib.cm.hot_r, size=None,
                    return_cbar=False, **kwargs):
    """
    Create a scatter plot with density of the data-points at each point on the
    x,y grid coded by the color in the colormap (hot per default)

    **kwargs are passed to matplotlib's matshow
    
    """

    x = np.copy(x)
    y = np.copy(y)
    
    max_x = np.nanmax(x)
    max_y = np.nanmax(y)
    min_x = np.nanmin(x)
    min_y = np.nanmin(y)
    
    x = ozu.rescale(x).ravel() * (res - 1) 
    y = ozu.rescale(y).ravel() * (res - 1)

    data_arr = np.zeros((res, res))

    for this_x,this_y in zip(x,y):
        # If one of them is a nan, move on:
        if np.isnan(this_x) or np.isnan(this_y):
            pass
        else: 
            data_arr[int(np.floor(this_x)), int(np.floor(this_y))] += 1

    
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    imax = ax.matshow(np.log10(np.flipud(data_arr.T)), cmap=cmap, **kwargs)    
    cbar = fig.colorbar(imax)
    ax.set_xticks([0] + [i * res/5.0 for i in range(5)])
    ax.set_yticks([0] + [i * res/5.0 for i in range(5)])
    ax.set_xticklabels([0] + ['%0.2f'%(i * ((max_x - min_x)/5.0) + min_x)
                               for i in range(5)])
    ax.set_yticklabels([0] + ['%0.2f'%(i * ((max_y - min_y)/5.0) + min_y)
                               for i in range(5,0,-1)])

    if size is not None:
        fig.set_size_inches(size)
    if return_cbar:
        return fig, cbar
    else: 
        return fig