Example #1
0
def update_colorbar_scale(figure, image, scale, vmin, vmax):
    """"
    Updates the colorbar to the scale and limits given.

    :param figure: A matplotlib figure instance
    :param image: The matplotlib image containing the colorbar
    :param scale: The norm scale of the colorbar, this should be a matplotlib colormap norm type
    :param vmin: the minimum value on the colorbar
    :param vmax: the maximum value on the colorbar
    """
    if vmin <= 0 and scale == LogNorm:
        vmin = 0.0001  # Avoid 0 log scale error
        mantid.kernel.logger.warning(
            "Scale is set to logarithmic so non-positive min value has been changed to 0.0001."
        )

    if vmax <= 0 and scale == LogNorm:
        vmax = 1  # Avoid 0 log scale error
        mantid.kernel.logger.warning(
            "Scale is set to logarithmic so non-positive max value has been changed to 1."
        )

    image.set_norm(scale(vmin=vmin, vmax=vmax))
    if image.colorbar:
        image.colorbar.remove()
        locator = None
        if scale == LogNorm:
            locator = LogLocator(subs=np.arange(1, 10))
            if locator.tick_values(vmin=vmin, vmax=vmax).size == 0:
                locator = LogLocator()
                mantid.kernel.logger.warning(
                    "Minor ticks on colorbar scale cannot be shown "
                    "as the range between min value and max value is too large"
                )
        figure.colorbar(image, ticks=locator)
Example #2
0
def get_ticks_log(vmin, vmax, numticks=None, extend='outer'):
    """ extend: how the min/max poses defined. 
            'outer': will try to extend it outwards (make min<vmin, max>vmax);
            'inner': will try to shrink it inwards;
    """

    from math import log10, ceil, floor
    from matplotlib.ticker import LogLocator

    if vmax > 0:
        vmin = max(vmin, min(10**int(log10(vmax/10)-1), 1e-3))
    else:
        vmax = 1.0
        vmin = 0.1

    if not numticks:
        numticks = 5

    if extend == 'outer':
        lvmin, lvmax = ceil(log10(vmin)), floor(log10(vmax))
    elif extend == 'inner':
        lvmin, lvmax = floor(log10(vmin)), ceil(log10(vmax))

    n = lvmax - lvmin
    stepm = max(1, floor(n/numticks))
    if n - stepm * numticks < (stepm+1)*numticks - n:
        step = stepm
    else:
        step = stepm+1

    ticks = 10.0**np.arange(lvmin, lvmax, step)   
    return ticks

    # The default scaling algorithm of MPL, not used

    subs = np.arange(int(10/numticks), 10, int(10/numticks), dtype=int) if numticks else (1.0,)
    locator = LogLocator(subs=subs)
    return [t for t in locator.tick_values(vmin, vmax) if t > vmin/10 and t < vmax*10]