@@ -2865,97 +2865,45 @@ def _load_cmap_cycle(filename, cmap=False):
28652865@_timer
28662866def register_cmaps ():
28672867 """
2868- Add colormaps packaged with ProPlot or saved to the ``~/.proplot/cmaps``
2869- folder. This is called on import. Maps are registered according to their
2870- filenames -- for example, ``name.xyz`` will be registered as ``'name'``.
2871-
2872- This is called on import. Use `show_cmaps` to generate a table of the
2873- registered colormaps. Valid extensions are described in the below table.
2874-
2875- ===================== =============================================================================================================================================================================================================
2876- Extension Description
2877- ===================== =============================================================================================================================================================================================================
2878- ``.hex`` List of HEX strings in any format (comma-separated, separate lines, with double quotes... anything goes).
2879- ``.xml`` XML files with ``<Point .../>`` entries specifying ``x``, ``r``, ``g``, ``b``, and optionally, ``a`` values, where ``x`` is the colormap coordinate and the rest are the RGB and opacity (or "alpha") values.
2880- ``.rgb`` 3-column table delimited by commas or consecutive spaces, each column indicating red, blue and green color values.
2881- ``.xrgb`` As with ``.rgb``, but with 4 columns. The first column indicates the colormap coordinate.
2882- ``.rgba``, ``.xrgba`` As with ``.rgb``, ``.xrgb``, but with a trailing opacity (or "alpha") column.
2883- ===================== =============================================================================================================================================================================================================
2884- """ # noqa
2885- # Turn original matplotlib maps from ListedColormaps
2886- # to LinearSegmentedColormaps. It makes zero sense to me that they are
2887- # stored as ListedColormaps.
2888- for name in CMAPS_CATEGORIES ['Matplotlib originals' ]:
2889- if name == 'twilight_shifted' : # CmapDict does this automatically
2890- mcm .cmap_d .pop (name , None )
2891- else :
2892- cmap = mcm .cmap_d .get (name , None )
2893- if isinstance (cmap , ListedColormap ):
2894- mcm .cmap_d .pop (name , None )
2895- mcm .cmap_d [name ] = LinearSegmentedColormap .from_list (
2896- name , cmap .colors , cyclic = (name == 'twilight' ))
2897-
2898- # Misc tasks
2899- # to be consistent with registered color names (also 'Murica)
2900- cmap = mcm .cmap_d .pop ('Greys' , None )
2901- if cmap is not None :
2902- mcm .cmap_d ['Grays' ] = cmap
2903- for name in ('Spectral' ,):
2904- mcm .cmap_d [name ] = mcm .cmap_d [name ].reversed (
2905- name = name ) # make spectral go from 'cold' to 'hot'
2906-
2907- # Remove gross cmaps (strong-arm user into using the better ones)
2908- for name in CMAPS_DELETE :
2909- mcm .cmap_d .pop (name , None )
2910-
2911- # Add colormaps from ProPlot and user directories
2912- for path in _get_data_paths ('cmaps' ):
2868+ Register colormaps packaged with ProPlot or saved to the
2869+ ``~/.proplot/cmaps`` folder. This is called on import. Maps are registered
2870+ according to their filenames -- for example, ``name.xyz`` will be
2871+ registered as ``'name'``.
2872+
2873+ For a table of valid extensions, see `LinearSegmentedColormap.from_file`.
2874+ To visualize the registered colormaps, use `show_cmaps`.
2875+ """
2876+ for i , path in enumerate (_get_data_paths ('cmaps' )):
29132877 for filename in sorted (glob .glob (os .path .join (path , '*' ))):
2914- name , cmap = _load_cmap_cycle (filename , cmap = True )
2915- if name is None :
2878+ cmap = LinearSegmentedColormap . from_file (filename )
2879+ if not cmap :
29162880 continue
2917- mcm .cmap_d [name ] = cmap
2918- # Add cyclic attribute
2919- for name , cmap in mcm .cmap_d .items ():
2920- # add hidden attribute used by BinNorm
2921- cmap ._cyclic = (name .lower () in (
2922- 'twilight' , 'twilight_shifted' , 'phase' , 'graycycle' ))
2881+ if i == 0 and cmap .name .lower () in ('phase' , 'graycycle' ):
2882+ cmap ._cyclic = True
2883+ mcm .cmap_d [cmap .name ] = cmap
29232884
29242885
29252886@_timer
29262887def register_cycles ():
29272888 """
2928- Add color cycles packaged with ProPlot or saved to the
2889+ Register color cycles packaged with ProPlot or saved to the
29292890 ``~/.proplot/cycles`` folder. This is called on import. Cycles are
29302891 registered according to their filenames -- for example, ``name.hex`` will
29312892 be registered under the name ``'name'`` as a
29322893 `~matplotlib.colors.ListedColormap` map (see `Cycle` for details).
29332894
2934- This is called on import. Use `show_cycles` to generate a table of the
2935- registered cycles. For valid file formats, see `register_cmaps `.
2895+ For a table of valid extensions, see `ListedColormap.from_file`.
2896+ To visualize the registered colormaps, use `show_cmaps `.
29362897 """
2937- # Remove gross cycles, change the names of some others
2938- for name in CYCLES_DELETE :
2939- mcm .cmap_d .pop (name , None )
2940- for (name1 , name2 ) in CYCLES_RENAME :
2941- cycle = mcm .cmap_d .pop (name1 , None )
2942- if cycle :
2943- mcm .cmap_d [name2 ] = cycle
2944-
2945- # Read cycles from directories
2946- cycles_load = {}
29472898 for path in _get_data_paths ('cycles' ):
29482899 for filename in sorted (glob .glob (os .path .join (path , '*' ))):
2949- name , data = _load_cmap_cycle (filename , cmap = False )
2950- if name is None :
2900+ cmap = ListedColormap . from_file (filename )
2901+ if not cmap :
29512902 continue
2952- cycles_load [name ] = data
2953-
2954- # Register cycles as ListedColormaps
2955- for name , colors in {** CYCLES_PRESET , ** cycles_load }.items ():
2956- cmap = ListedColormap (colors , name = name )
2957- cmap .colors = [to_rgb (color , alpha = True ) for color in cmap .colors ]
2958- mcm .cmap_d [name ] = cmap
2903+ if isinstance (cmap , LinearSegmentedColormap ):
2904+ cmap = ListedColormap (colors (cmap ), name = cmap .name )
2905+ mcm .cmap_d [cmap .name ] = cmap
2906+ cycles .append (cmap .name )
29592907
29602908
29612909@_timer
@@ -2977,7 +2925,7 @@ def register_colors(nmax=np.inf):
29772925 colors .clear ()
29782926 base = {}
29792927 base .update (mcolors .BASE_COLORS )
2980- base .update (COLORS_BASE )
2928+ base .update (COLORS_BASE ) # full names
29812929 mcolors .colorConverter .colors .clear () # clean out!
29822930 mcolors .colorConverter .cache .clear () # clean out!
29832931 for name , dict_ in (('base' , base ), ('css' , mcolors .CSS4_COLORS )):
@@ -3005,7 +2953,8 @@ def register_colors(nmax=np.inf):
30052953 if not all (len (pair ) == 2 for pair in pairs ):
30062954 raise RuntimeError (
30072955 f'Invalid color names file { file !r} . '
3008- f'Every line must be formatted as "name: color".' )
2956+ f'Every line must be formatted as "name: color".'
2957+ )
30092958
30102959 # Categories for which we add *all* colors
30112960 if cat == 'open' or i == 1 :
@@ -3048,9 +2997,9 @@ def register_colors(nmax=np.inf):
30482997
30492998@_timer
30502999def register_fonts ():
3051- """Adds fonts packaged with ProPlot or saved to the ``~/.proplot/fonts``
3052- folder. Also deletes the font cache, which may cause delays.
3053- Detects ``.ttf`` and ``.otf`` files -- see `this link \
3000+ """Add fonts packaged with ProPlot or saved to the ``~/.proplot/fonts``
3001+ folder, if they are not already added. Detects ``.ttf`` and ``.otf`` files
3002+ -- see `this link \
30543003 <https://gree2.github.io/python/2015/04/27/python-change-matplotlib-font-on-mac>`__
30553004 for a guide on converting various other font file types to ``.ttf`` and
30563005 ``.otf`` for use with matplotlib."""
@@ -3621,6 +3570,32 @@ def show_fonts(*args, size=12, text=None):
36213570 return f
36223571
36233572
3573+ # Apply custom changes
3574+ mcm .cmap_d ['Grays' ] = mcm .cmap_d .pop ('Greys' , None ) # 'Murica (and consistency with registered colors) # noqa
3575+ mcm .cmap_d ['Spectral' ] = mcm .cmap_d ['Spectral' ].reversed (
3576+ name = 'Spectral' ) # make spectral go from 'cold' to 'hot'
3577+ for _name in CMAPS_TABLE ['Matplotlib originals' ]: # initialize as empty lists
3578+ if _name == 'twilight_shifted' :
3579+ mcm .cmap_d .pop (_name , None )
3580+ else :
3581+ _cmap = mcm .cmap_d .get (_name , None )
3582+ if _cmap and isinstance (_cmap , mcolors .ListedColormap ):
3583+ mcm .cmap_d .pop (_name , None ) # removes the map from cycles list!
3584+ mcm .cmap_d [_name ] = LinearSegmentedColormap .from_list (
3585+ _name , _cmap .colors , cyclic = ('twilight' in _name ))
3586+ for _cat in ('MATLAB' , 'GNUplot' , 'GIST' , 'Other' ):
3587+ for _name in CMAPS_TABLE [_cat ]:
3588+ mcm .cmap_d .pop (_name , None )
3589+
3590+ # Initialize customization folders and files
3591+ _rc_folder = os .path .join (os .path .expanduser ('~' ), '.proplot' )
3592+ if not os .path .isdir (_rc_folder ):
3593+ os .mkdir (_rc_folder )
3594+ for _rc_sub in ('cmaps' , 'cycles' , 'colors' , 'fonts' ):
3595+ _rc_sub = os .path .join (_rc_folder , _rc_sub )
3596+ if not os .path .isdir (_rc_sub ):
3597+ os .mkdir (_rc_sub )
3598+
36243599#: List of registered colormap names.
36253600cmaps = [] # track *downloaded* colormaps
36263601
0 commit comments