def get_cmap(name, lut=None): """ Returns the specified colormap. Parameters ---------- name: str or :class:`matplotlib.colors.Colormap` If a colormap, it returned unchanged. %(cmap_note)s lut: int An integer giving the number of entries desired in the lookup table Returns ------- matplotlib.colors.Colormap The colormap specified by `name` See Also -------- show_colormaps: A function to display all available colormaps Notes ----- Different from the :func::`matpltolib.pyplot.get_cmap` function, this function changes the number of colors if `name` is a :class:`matplotlib.colors.Colormap` instance to match the given `lut`.""" if name in rcParams['colors.cmaps']: colors = rcParams['colors.cmaps'][name] lut = lut or len(colors) return FixedColorMap.from_list(name=name, colors=colors, N=lut) elif name in _cmapnames: colors = _cmapnames[name] lut = lut or len(colors) return FixedColorMap.from_list(name=name, colors=colors, N=lut) else: cmap = mpl_get_cmap(name) # Note: we could include the `lut` in the call of mpl_get_cmap, but # this raises a ValueError for colormaps like 'viridis' in mpl version # 1.5. Besides the mpl_get_cmap function does not modify the lut if # it does not match if lut is not None and cmap.N != lut: cmap = FixedColorMap.from_list(name=cmap.name, colors=cmap(np.linspace(0, 1, lut)), N=lut) return cmap
def get_cmap(name, lut=None): """ Returns the specified colormap. Parameters ---------- name: str or :class:`matplotlib.colors.Colormap` If a colormap, it returned unchanged. %(cmap_note)s lut: int An integer giving the number of entries desired in the lookup table Returns ------- matplotlib.colors.Colormap The colormap specified by `name` See Also -------- show_colormaps: A function to display all available colormaps Notes ----- Different from the :func::`matpltolib.pyplot.get_cmap` function, this function changes the number of colors if `name` is a :class:`matplotlib.colors.Colormap` instance to match the given `lut`.""" if name in rcParams['colors.cmaps']: colors = rcParams['colors.cmaps'][name] lut = lut or len(colors) return FixedColorMap.from_list(name=name, colors=colors, N=lut) elif name in _cmapnames: colors = _cmapnames[name] lut = lut or len(colors) return FixedColorMap.from_list(name=name, colors=colors, N=lut) else: cmap = mpl_get_cmap(name) # Note: we could include the `lut` in the call of mpl_get_cmap, but # this raises a ValueError for colormaps like 'viridis' in mpl version # 1.5. Besides the mpl_get_cmap function does not modify the lut if # it does not match if lut is not None and cmap.N != lut: cmap = FixedColorMap.from_list( name=cmap.name, colors=cmap(np.linspace(0, 1, lut)), N=lut) return cmap
'white_red_blue': [ # water fluxes (1, 1., 1), (1, 0, 0), (1, 1, 0), (0, 1, 1), (0, 0, 1)], 'rwb': [ (1, 0, 0), (1, 1., 1), (0, 0, 1)], 'wrb': [ (1, 1., 1), (1, 0, 0), (0, 0, 1)], 'wbr': [ (1, 1., 1), (0, 0, 1), (1, 0, 0)] } for key, val in list(_cmapnames.items()): _cmapnames[key + '_r'] = val[::-1] _color_array = np.linspace(0, 1, 256, endpoint=True) _cmapnames['w_RdBu'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('RdBu')(_color_array), axis=0) _cmapnames['w_RdBu_r'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('RdBu_r')(_color_array), axis=0) _cmapnames['w_Reds'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('Reds')(_color_array), axis=0) _cmapnames['w_Blues'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('Blues')(_color_array), axis=0) _cmapnames['w_Greens'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('Greens')(_color_array), axis=0) docstrings.params['cmap_note'] = """ Strings may be any valid colormap name suitable for the
'white_red_blue': [ # water fluxes (1, 1., 1), (1, 0, 0), (1, 1, 0), (0, 1, 1), (0, 0, 1)], 'rwb': [ (1, 0, 0), (1, 1., 1), (0, 0, 1)], 'wrb': [ (1, 1., 1), (1, 0, 0), (0, 0, 1)], 'wbr': [ (1, 1., 1), (0, 0, 1), (1, 0, 0)] } for key, val in list(_cmapnames.items()): _cmapnames[key + '_r'] = val[::-1] _color_array = np.linspace(0, 1, 256, endpoint=True) _cmapnames['w_RdBu'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('RdBu')(_color_array), axis=0) _cmapnames['w_RdBu_r'] = np.append( [[1., 1., 1., 1.]], mpl_get_cmap('RdBu_r')(_color_array), axis=0) docstrings.params['cmap_note'] = """ Strings may be any valid colormap name suitable for the :func:`matplotlib.cm.get_cmap` function or one of the color lists defined in the 'colors.cmaps' key of the :attr:`psyplot.rcParams` dictionary (including their reversed color maps given via the '_r' extension).""" class FixedColorMap(LinearSegmentedColormap): """Bug fixing colormap with same functionality as matplotlibs colormap
(1, 1., 1), (0, 0, 1), (0, 1, 1), (1, 1, 0), (1, 0, 0) ], 'white_red_blue': [ # water fluxes (1, 1., 1), (1, 0, 0), (1, 1, 0), (0, 1, 1), (0, 0, 1) ], 'rwb': [(1, 0, 0), (1, 1., 1), (0, 0, 1)], 'wrb': [(1, 1., 1), (1, 0, 0), (0, 0, 1)], 'wbr': [(1, 1., 1), (0, 0, 1), (1, 0, 0)] } for key, val in list(_cmapnames.items()): _cmapnames[key + '_r'] = val[::-1] _color_array = np.linspace(0, 1, 256, endpoint=True) _cmapnames['w_RdBu'] = np.append([[1., 1., 1., 1.]], mpl_get_cmap('RdBu')(_color_array), axis=0) _cmapnames['w_RdBu_r'] = np.append([[1., 1., 1., 1.]], mpl_get_cmap('RdBu_r')(_color_array), axis=0) _cmapnames['w_Reds'] = np.append([[1., 1., 1., 1.]], mpl_get_cmap('Reds')(_color_array), axis=0) _cmapnames['w_Blues'] = np.append([[1., 1., 1., 1.]], mpl_get_cmap('Blues')(_color_array), axis=0) _cmapnames['w_Greens'] = np.append([[1., 1., 1., 1.]], mpl_get_cmap('Greens')(_color_array),