def _generate_cmap(name, lutsize): """Generates the requested cmap from it's name *name*. The lut size is *lutsize*.""" spec = datad[name] # Generate the colormap object. if 'red' in spec: return colors.LinearSegmentedColormap(name, spec, lutsize) else: return colors.LinearSegmentedColormap.from_list(name, spec, lutsize) LUTSIZE = mpl.rcParams['image.lut'] _cmapnames = datad.keys() # need this list because datad is changed in loop # Generate the reversed specifications ... for cmapname in _cmapnames: spec = datad[cmapname] spec_reversed = _reverse_cmap_spec(spec) datad[cmapname + '_r'] = spec_reversed # Precache the cmaps with ``lutsize = LUTSIZE`` ... # Use datad.keys() to also add the reversed ones added in the section above: for cmapname in datad.iterkeys(): cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE) locals().update(cmap_d)
def _generate_cmap(name, lutsize): """Generates the requested cmap from it's name *name*. The lut size is *lutsize*.""" spec = datad[name] # Generate the colormap object. if 'red' in spec: return colors.LinearSegmentedColormap(name, spec, lutsize) else: return colors.LinearSegmentedColormap.from_list(spec, spec, lutsize) LUTSIZE = mpl.rcParams['image.lut'] _cmapnames = datad.keys() # need this list because datad is changed in loop # Generate the reversed specifications ... for cmapname in _cmapnames: spec = datad[cmapname] spec_reversed = _reverse_cmap_spec(spec) datad[cmapname + '_r'] = spec_reversed # Precache the cmaps with ``lutsize = LUTSIZE`` ... # Use datad.keys() to also add the reversed ones added in the section above: for cmapname in datad.keys(): cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE) locals().update(cmap_d)
import netCDF4 as nc import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib.animation import FuncAnimation from matplotlib._cm_listed import cmaps as cmaps_listed # dict of colors with color objects from matplotlib._cm import datad # dict of colors with actual maps pdsi = nc.Dataset('f:/data/droughtindices/netcdfs/pdsi.nc') array = pdsi.variables['value'][:] dates = pdsi.variables['time'][:] base = dt.datetime(1900, 1, 1) dates2 = [base + dt.timedelta(d) for d in dates] dates3 = ['PDSI: ' + dt.datetime.strftime(d, "%Y-%m") for d in dates2] ckeys = list(cmaps_listed.keys()) [ckeys.insert(0, d) for d in list(datad.keys())] ckeys.sort() # Matplotlib: def movie(array, titles=None, axis=0): ''' if the time axis is not 0, specify which it is. ''' if titles is None: titles = ["" for t in range(len(array))] if type(titles) is str: titles = [titles + ': ' + str(t) for t in range(len(array))] fig, ax = plt.subplots()
# 'gnuplot', 'gnuplot2', 'CMRmap', 'cubehelix', 'brg', 'hsv', # 'gist_rainbow', 'rainbow', 'jet', 'nipy_spectral', 'gist_ncar'])] def cmap_fun(cm): def wrapper(x, alpha=None, bytes=False, lut=None, clim=None, norm=None): cmap = plt.get_cmap(cm, lut=lut) if norm is None: clim = clim or (0., 1.) norm = plt.Normalize(vmin=clim[0], vmax=clim[1]) x = norm(x) return cmap(x, alpha=alpha, bytes=bytes) return wrapper # explicte declaration (for example) def jet(x, alpha=None, bytes=False, lut=None, clim=None, norm=None): return cmap_fun('jet')(x, alpha=alpha, bytes=bytes, lut=lut, clim=clim, norm=norm) # fill functions locals().update({cm: cmap_fun(cm) for cm in datad.keys()}) locals().update({cm + '_r': cmap_fun(cm + '_r') for cm in datad.keys()})
"""Generates the requested cmap from it's name *name*. The lut size is *lutsize*.""" spec = datad[name] # Generate the colormap object. if 'red' in spec: return colors.LinearSegmentedColormap(name, spec, lutsize) else: return colors.LinearSegmentedColormap.from_list(name, spec, lutsize) LUTSIZE = mpl.rcParams['image.lut'] _cmapnames = list( datad.keys()) # need this list because datad is changed in loop # Generate the reversed specifications ... for cmapname in _cmapnames: spec = datad[cmapname] spec_reversed = _reverse_cmap_spec(spec) datad[cmapname + '_r'] = spec_reversed # Precache the cmaps with ``lutsize = LUTSIZE`` ... # Use datad.keys() to also add the reversed ones added in the section above: for cmapname in datad.keys(): cmap_d[cmapname] = _generate_cmap(cmapname, LUTSIZE) locals().update(cmap_d)
import re import copy import numpy as np import seaborn as sns import matplotlib.pyplot as plt import matplotlib.cm as cm import matplotlib.colors as colors from matplotlib._cm import datad from mpl_toolkits.mplot3d import Axes3D from .generic_utils import handleKeyError from .metric_utils import silhouette_samples from .metric_utils import silhouette_score ALL_CMAP_NAMES = [ name for name in datad.keys() if not re.search(r".*_r", name, re.IGNORECASE) ] COLOR_TYPES = ["rgba", "rgb", "hex"] def rgb2hex(rgb, max_val=1): return "#" + "".join([format(int(255 / max_val * e), '02x') for e in rgb]).upper() def rgba2rgb(rgba, max_val=1): alpha = rgba[-1] rgb = rgba[:-1] type_ = int if max_val == 255 else float # compute the color as alpha against white