cmcrameri

repository·main·Indexed 42 days ago

https://github.com/callumrollo/cmcrameri

A Python wrapper for Fabio Crameri's perceptually uniform colormaps, providing integration with Matplotlib for scientific visualization. It includes sequential, diverging, multi-sequential, cyclic, and categorical colormaps, accessible via the cmcrameri.cm module or registered Matplotlib name strings prefixed with 'cmc.'.

Tokens
1.3K
Snippets
9
Records
11
Agent score
52%

What's inside cmcrameri

  1. Access and identify colormaps

    main

    All core colormaps are available via cmcrameri.cm.<colormapname>.

    Naming conventions:

    • Reversed colormaps: Append _r to the name (e.g., batlow_r).
    • Categorical colormaps: These have the suffix S (e.g., batlowS).

    You can use tab autocompletion on cmcrameri.cm in editors that support it to browse available names.

  2. Use cmcrameri colormaps with Matplotlib

    main

    To use the colormaps in Matplotlib, import cmcrameri.cm as cmc. You can pass the colormap object directly to the cmap argument of plotting functions like plt.imshow().

    To use a colormap by its registered name string (e.g., 'cmc.batlow'), you must first import cmcrameri to ensure the colormaps are registered with Matplotlib.

    import cmcrameri.cm as cmc
    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0, 1, 100)[np.newaxis, :]
    
    # Using the colormap object directly
    plt.imshow(x, aspect='auto', cmap=cmc.batlow)
    plt.axis('off')
    plt.show()
    
    # Using the registered name string (requires 'import cmcrameri')
    import cmcrameri
    plt.imshow(x, aspect='auto', cmap='cmc.batlow')
  3. Register cmcrameri colormaps in Matplotlib

    main

    The cmcrameri.cm module automatically loads and registers a collection of perceptually uniform colormaps from Fabio Crameri. When registered, these colormaps are prefixed with cmc. to avoid namespace collisions in Matplotlib.

    To use them, you can either access them directly from the cmcrameri.cm namespace or refer to them by their registered names (e.g., 'cmc.batlow') in any Matplotlib plotting function.

    import matplotlib.pyplot as plt
    import cmcrameri.cm
    
    # Use a colormap by its registered name
    plt.imshow(data, cmap='cmc.batlow')
    
    # Or use it directly from the namespace
    plt.imshow(data, cmap=cmcrameri.cm.batlow)
  4. Visualize all available cmcrameri colormaps

    main

    You can use the show_cmaps function to generate a visual grid of all available colormaps, organized by category (Sequential, Diverging, Multi-sequential, and Cyclic). This is useful for inspecting the color scales before choosing one for your plot.

    import cmcrameri.cm
    import matplotlib.pyplot as plt
    
    cmcrameri.cm.show_cmaps(ncols=6, figwidth=10)
    plt.show()
  5. Access colormaps via the cm module

    main

    The cm module is the primary entrypoint for accessing the collection of perceptually uniform colormaps. It serves as a wrapper for colormaps created by Fabio Crameri. You can import cm from the top-level cmcrameri package to access the available colormaps.

    import cmcrameri
    
    # Access colormaps through the cm module
    # Example: cmcrameri.cm.batlow
  6. Reference: cmcrameri colormap categories and naming

    main

    Colormaps are categorized into four main types. Note that categorical and cyclic versions have specific suffixes in their names:

    • Sequential: Standard perceptually uniform scales (e.g., batlow, devon, nuuk).
    • Diverging: Scales designed to highlight deviations from a central value (e.g., broc, cork, vik).
    • Multi-sequential: Scales with multiple sequential segments (e.g., oleron, bukavu, fes).
    • Cyclic: Scales designed for periodic data, suffixed with O (e.g., romaO, bamO).
    • Categorical: Discrete scales suffixed with S (e.g., batlowS).

    All registered names in Matplotlib are prefixed with cmc..