Python matplotlib.cm.register_cmap() Examples

The following are code examples for showing how to use matplotlib.cm.register_cmap(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pseudonetcdf   Author: barronh   File: colors.py    GNU Lesser General Public License v3.0 6 votes vote down vote up
def _addusepaaqi():
    aqicolors = ['green', 'yellow', 'orange', 'red', 'purple', 'maroon']
    aqic = aqicolors
    bounds_colors = {}
    bounds_colors[('O3', 'mda8', 'ppbv')] = ([0, 55, 71, 86, 106, 200],
                                             aqic[:5] + ['black'], 'max')
    bounds_colors[('O3', 'mda8', 'ppmv')] = ([0, 0.055, 0.071, 0.086,
                                              0.106, 0.0200],
                                             aqic[:5] + ['black'], 'max')
    bounds_colors[('O3', 'mda1', 'ppbv')] = ([125, 165, 205, 405, 604],
                                             ['white'] + aqic[2:] + ['black'],
                                             'both')
    pmb = [0, 12.1, 35.5, 55.5, 150.5, 250.5, 500.4]
    pmc = aqic + ['black']
    bounds_colors[('PM25', 'a24', 'micrograms/m**3')] = (pmb, pmc, 'max')
    for key, (bnds, colors, extend) in bounds_colors.items():
        print(key)
        name = '_'.join(('usepa', 'aqi',) + key)
        cmap, norm = from_levels_and_colors(bnds, colors, extend=extend)
        register_cmap(name=name, cmap=cmap)
        register_norm(name=name, norm=norm) 
Example 2
Project: jopy   Author: jowr   File: mplib.py    MIT License 6 votes vote down vote up
def _register_color_maps(self):
        BaseStyle._register_color_maps(self)
        
        cc    = mpl.colors.ColorConverter()
        redwhite    = [cc.to_rgba('#FF0000',alpha=1),cc.to_rgba('#FFFFFF',alpha=1)]
        greenwhite  = [cc.to_rgba('#99CC33',alpha=1),cc.to_rgba('#FFFFFF',alpha=1)]
        purplewhite = [cc.to_rgba('#660099',alpha=1),cc.to_rgba('#FFFFFF',alpha=1)]
        yellowwhite = [cc.to_rgba('#FFCC00',alpha=1),cc.to_rgba('#FFFFFF',alpha=1)]
        # create map and register it together with reversed colours
        maps = []
        maps.append(LinearSegmentedColormap.from_list('DTU1', redwhite))
        maps.append(LinearSegmentedColormap.from_list('DTU1_r', redwhite[::-1]))
        maps.append(LinearSegmentedColormap.from_list('DTU2', greenwhite))
        maps.append(LinearSegmentedColormap.from_list('DTU2_r', greenwhite[::-1]))
        maps.append(LinearSegmentedColormap.from_list('DTU3', purplewhite))
        maps.append(LinearSegmentedColormap.from_list('DTU3_r', purplewhite[::-1]))
        maps.append(LinearSegmentedColormap.from_list('DTU4', yellowwhite))
        maps.append(LinearSegmentedColormap.from_list('DTU4_r', yellowwhite[::-1]))
        
        for cmap in maps:
            mplcm.register_cmap(cmap=cmap)
            self._color_maps[cmap.name] = cmap 
Example 3
Project: jopy   Author: jowr   File: mplib.py    MIT License 6 votes vote down vote up
def _register_color_maps(self):
        BaseStyle._register_color_maps(self)
        rgb = [
          (  0./255. ,   0./255. ,   0./255.),     
          (  0./255. , 102./255. ,  51./255.),
          (114./255. , 121./255. , 126./255.),
          ( 91./255. , 172./255. ,  38./255.),
          (217./255. , 220./255. , 222./255.),
          (255./255. , 255./255. , 255./255.)
          ]

        # create map and register it together with reversed colours
        maps = []
        maps.append(LinearSegmentedColormap.from_list('IPU'  , rgb))
        maps.append(LinearSegmentedColormap.from_list('IPU_r', rgb[::-1]))
    
        for cmap in maps:
            mplcm.register_cmap(cmap=cmap)
            self._color_maps[cmap.name] = cmap 
Example 4
Project: jopy   Author: jowr   File: mplib.py    MIT License 6 votes vote down vote up
def _register_color_maps(self):
        BaseStyle._register_color_maps(self)
        rgb = [
          ( 25./255. ,  13./255. ,  25./255.),
          (  0./255. ,  51./255. , 102./255.),     
          #(205./255. ,  51./255. ,  51./255.),
          (  0./255. , 109./255. , 148./255.),
          (127./255. , 186./255. ,  50./255.),
          (255./255. , 255./255. , 255./255.)]

        # create map and register it together with reversed colours
        maps = []
        maps.append(LinearSegmentedColormap.from_list('VDG'  , rgb))
        maps.append(LinearSegmentedColormap.from_list('VDG_r', rgb[::-1]))
    
        for cmap in maps:
            mplcm.register_cmap(cmap=cmap)
            self._color_maps[cmap.name] = cmap 
Example 5
Project: xcube   Author: dcs4cop   File: test_cmaps.py    MIT License 5 votes vote down vote up
def test_get_cmaps_registers_snap_color(self):
        ensure_cmaps_loaded()
        cmap_name = os.path.join(os.path.dirname(__file__), 'chl_DeM2_200.cpd')
        cmap = _get_custom_colormap(cmap_name)
        cm.register_cmap(cmap=cmap)
        self.assertTrue((type(cmap) is LinearSegmentedColormap) or (type(cmap) is ListedColormap)) 
Example 6
Project: xcube   Author: dcs4cop   File: test_cmaps.py    MIT License 5 votes vote down vote up
def test_get_cmaps_registers_ivalid_snap_color(self):
        ensure_cmaps_loaded()
        cmap_name = os.path.join(os.path.dirname(__file__), 'chl_DeM2_200_invalid_for_testing.cpd')
        with self.assertRaises(ValueError):
            cmap = _get_custom_colormap(cmap_name)
            cm.register_cmap(cmap=cmap) 
Example 7
Project: xcube   Author: dcs4cop   File: test_cmaps.py    MIT License 5 votes vote down vote up
def test_get_cmaps_registers_nonexisting_snap_color(self):
        ensure_cmaps_loaded()
        cmap_name = 'test/webapi/im/chl_DeM2_200_not_existing.cpd'
        with self.assertRaises(ValueError):
            cmap = _get_custom_colormap(cmap_name)
            cm.register_cmap(cmap=cmap) 
Example 8
Project: viewer-demo   Author: CCI-Tools   File: cmaps.py    GNU General Public License v3.0 4 votes vote down vote up
def get_cmaps():
    """
    Return a tuple containing records of the form: (<cmap-category>, <cmap-category-description>, <cmap-tuples>),
    where <cmap-tuples> is a tuple containing records of the form (<cmap-name>, <cbar-png-bytes>), and where
    <cbar-png-bytes> are encoded PNG images of size 256 x 2 pixels,
    :return: all known matplotlib color maps
    """
    global _CBARS_LOADED, _CMAPS
    if not _CBARS_LOADED:
        _LOCK.acquire()
        _CBARS_LOADED = True
        new_cmaps = []
        for cmap_category, cmap_description, cmap_names in _CMAPS:
            cbar_list = []
            for cmap_name in cmap_names:
                try:
                    cmap = cm.get_cmap(cmap_name)
                except:
                    print("ERROR: invalid colormap '" + cmap_name + "'")
                    continue

                # Add extra colormaps with alpha gradient
                # see http://matplotlib.org/api/colors_api.html
                if type(cmap) == matplotlib.colors.LinearSegmentedColormap:
                    new_name = cmap.name + '_alpha'
                    new_segmentdata = dict(cmap._segmentdata)
                    # let alpha increase from 0.0 to 0.5
                    new_segmentdata['alpha'] = ((0.0, 0.0, 0.0),
                                                (0.5, 1.0, 1.0),
                                                (1.0, 1.0, 1.0))
                    new_cmap = matplotlib.colors.LinearSegmentedColormap(new_name, new_segmentdata)
                    cm.register_cmap(cmap=new_cmap)
                    print("INFO: new colormap '" + new_name + "'")
                elif type(cmap) == matplotlib.colors.ListedColormap:
                    new_name = cmap.name + '_alpha'
                    print("TODO: create colormap '" + new_name + "'")

                gradient = np.linspace(0, 1, 256)
                gradient = np.vstack((gradient, gradient))
                image_data = cmap(gradient, bytes=True)
                image = Image.fromarray(image_data, 'RGBA')

                # ostream = io.FileIO('../cmaps/' + cmap_name + '.png', 'wb')
                # image.save(ostream, format='PNG')
                # ostream.close()

                ostream = io.BytesIO()
                image.save(ostream, format='PNG')
                cbar_png_bytes = ostream.getvalue()
                ostream.close()

                cbar_png_data = base64.b64encode(cbar_png_bytes)
                cbar_png_bytes = cbar_png_data.decode('unicode_escape')

                cbar_list.append((cmap_name, cbar_png_bytes))
            new_cmaps.append((cmap_category, cmap_description, tuple(cbar_list)))
        _CMAPS = tuple(new_cmaps)
        _LOCK.release()
        #import pprint
        #pprint.pprint(_CMAPS)
    return _CMAPS