Exemplo n.º 1
0
def save_colorbar(scale, path, colormap='default'):
    '''
    Save a plt colorbar with a given scale to a specified path.  Accepts plt or cv2 colormap objects.

    Arguments:
        scale: 
            The scale dictionary returned by rescale_movie.  Must have keys 'min' and 'max' providing the range of the rescale.
        path: 
            Where to save the file to. (supported formats: eps, jpeg, jpg, pdf, pgf, png, ps, raw, rgba, svg, svgz, tif, tiff)
        colormap: 
            Which colormap to save.  Should be a cv2 colormap object or name of a plt colormap.  If left as 'default', the default colormap will be loaded.  

    Returns:
        Nothing

    Raises:
        KeyError: 
            The min and/or max keys were not in scale dictionary
        ValueError: 
            The path provided was not supported by plt figure outputs.
    '''
    if type(colormap) is str:
        if colormap == 'default':
            colormap = DEFAULT_COLORMAP

    if type(colormap) is np.ndarray:
        colormap = ListedColormap(colormap.squeeze() / 256)

    ticks = np.linspace(scale['min'], scale['max'], 5).round(4)

    plt.figure(figsize=(1, 2))
    plt.imshow([[0, 0], [0, 0]],
               vmin=scale['min'],
               vmax=scale['max'],
               cmap=colormap)

    cb = plt.colorbar()
    cb.set_ticks(ticks)

    plt.cla()
    plt.axis('off')

    plt.savefig(path, bbox_inches='tight')
    plt.close()