colormaps.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:notebook-molecular-visualization 作者: Autodesk 项目源码 文件源码
def colormap(cats, mplmap='auto', categorical=None):
    """ Map a series of categories to hex colors, using a matplotlib colormap

    Generates both categorical and numerical colormaps.

    Args:
        cats (Iterable): list of categories or numerical values
        mplmap (str): name of matplotlib colormap object
        categorical (bool): If None
            (the default) interpret data as numerical only if it can be cast to float.
            If True, interpret this data as categorical. If False, cast the data to float.

    Returns:
        List[str]: List of hexadecimal RGB color values in the in the form ``'#000102'``
    """
    # Should automatically choose the right colormaps for:
    #  categorical data
    #  sequential data (low, high important)
    #  diverging data (low, mid, high important)
    global DEF_SEQUENTIAL
    from matplotlib import cm

    if hasattr(cm, 'inferno'):
        DEF_SEQUENTIAL = 'inferno'
    else:
        DEF_SEQUENTIAL = 'BrBG'

    # strip units
    units = None  # TODO: build a color bar with units
    if hasattr(cats[0], 'magnitude'):
        arr = u.array(cats)
        units = arr.units
        cats = arr.magnitude
        is_categorical = False
    else:
        is_categorical = not isinstance(cats[0], (float, int))

    if categorical is not None:
        is_categorical = categorical

    if is_categorical:
        values = _map_categories_to_ints(cats)
        if mplmap == 'auto':
            mplmap = DEF_CATEGORICAL
    else:
        values = np.array(list(map(float, cats)))
        if mplmap == 'auto':
            mplmap = DEF_SEQUENTIAL

    rgb = _cmap_to_rgb(mplmap, values)
    hexcolors = [webcolors.rgb_to_hex(np.array(c)) for c in rgb]
    return hexcolors
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号