cmocean

repository·main·Indexed 42 days ago

https://github.com/matplotlib/cmocean

A collection of perceptually uniform colormaps designed for oceanographic and scientific data visualization. It provides sequential, diverging, circular, and composite colormaps, including specialized tools for color management and integration with Matplotlib.

Tokens
2.7K
Snippets
6
Records
14
Agent score
55%

What's inside cmocean

  1. Overview of cmocean colormaps

    main

    cmocean is a collection of perceptually uniform colormaps designed for oceanographic and scientific visualization. The colormaps are based on guidelines for color selection in oceanography as described in:

    Thyng, K. M., Greene, C. A., Hetland, R. D., Zimmerle, H. M., & DiMarco, S. F. (2016). True colors of oceanography. Oceanography, 29(3), 10.

    Beyond Python, these colormaps are available in several other environments including MATLAB, R, Julia, Plotly, Tableau, and more.

  2. Overview of the cmocean package structure

    main

    The cmocean package is organized into several functional submodules designed for oceanographic data visualization and color management:

    • cmocean.cmocean: The core module containing colormaps specifically designed for oceanographic applications.
    • cmocean.plots: Provides high-level plotting utilities and functions to simplify visualization tasks.
    • cmocean.tools: Contains utility functions and tools to assist with data manipulation or color management.
    • cmocean (top-level): The main entry point that exposes the package's primary functionality.
  3. External resources and integrations for cmocean

    main

    cmocean colormaps are available across various scientific and data visualization platforms. If you are working outside of a Python/Matplotlib environment, you can find implementations for:

    • MATLAB: Original implementation by Chad Greene.
    • R: Via the Oce oceanographic analysis package.
    • Generic Mapping Tools (GMT): Available via cpt-city and GitHub.
    • Other Tools: Paraview, Plotly, Tableau (via preference files), and ImageJ (via LUTs).
    • Other Languages: Included in the julia package.
  4. Use reversed and inverted lightness colormaps

    main

    You can modify existing colormaps using standard suffixes:

    • Reversed: Append _r to the name (e.g., cmocean.cm.gray_r).
    • Inverted Lightness: Append _i to the name. This is useful for diverging colormaps used on light backgrounds (e.g., cmocean.cm.balance_i).
    • Combined: You can combine both, such as _i_r or _r_i (e.g., cmocean.cm.balance_i_r).
    import cmocean.cm as cmo
    
    # Reversed
    cmap_rev = cmo.gray_r
    
    # Inverted lightness
    cmap_inv = cmo.balance_i
    
    # Both
    cmap_both = cmo.balance_i_r
  5. Best practices for choosing colormaps

    main

    When using cmocean, it is important to choose the correct type of colormap based on the nature of your data. The project emphasizes moving away from the jet colormap due to its non-perceptual nature and tendency to create visual artifacts.

    Key considerations for choosing a colormap include:

    • Perceptual Uniformity: Use colormaps that represent data changes linearly in perception to avoid artifacts.
    • Data Type: Determine if your data is sequential (e.g., temperature, depth) or diverging (e.g., anomalies from a mean, political leaning).
    • Accessibility: Consider color blindness by using tools like a colourblind simulator to ensure your visualizations are readable by all users.
  6. Install cmocean

    main

    You can install cmocean using pip or conda. If you require the plots submodule (which includes visualization tools like viscm and colorspacious), install the [plots] extra.

    Standard Installation:

    pip install cmocean

    Anaconda Installation:

    conda install -c conda-forge cmocean

    Installation with plotting capabilities:

    pip install "cmocean[plots]"
    # Standard installation
    pip install cmocean
    
    # Installation with plotting capabilities
    pip install "cmocean[plots]"
  7. Access and use cmocean colormaps

    main

    All oceanographic colormaps are located in the cmocean.cm module. You can access them directly as objects or use their registered names within matplotlib by prefixing them with cmo..

    Direct access: Use cmocean.cm.name (e.g., cmocean.cm.thermal).

    Matplotlib integration: Once imported, you can use the string name in matplotlib functions: cmap='cmo.name' (e.g., cmap='cmo.amp').

    Available names: You can retrieve a list of all available colormap names using cmocean.cm.cmapnames.

    import cmocean
    import matplotlib.pyplot as plt
    import numpy as np
    
    Z = np.random.randn(10,10)
    fig, ax = plt.subplots()
    
    # Method 1: Direct object access
    ax.pcolormesh(Z, cmap=cmocean.cm.thermal)
    
    # Method 2: Using registered matplotlib string
    ax.pcolormesh(Z, cmap='cmo.amp')
    
    # To see all names:
    print(cmocean.cm.cmapnames)
  8. Visualize colormaps using cmocean.plots

    main

    You can quickly inspect and test cmocean colormaps using the cmocean.plots module. The test method provides a standard visualization, while quick_plot offers an alternative view. This is useful for verifying how a colormap will appear in your specific plots.

    import cmocean
    import matplotlib.pyplot as plt
    
    fig = plt.figure(figsize=(8, 3))
    ax = fig.add_subplot(1, 2, 1)
    cmocean.plots.test(cmocean.cm.gray, ax=ax)
    ax = fig.add_subplot(1, 2, 2)
    cmocean.plots.quick_plot(cmocean.cm.gray, ax=ax)
  9. Visualize cmocean colormaps using the viscm tool

    main

    You can visualize all available cmocean colormaps using the cmocean.plots.wrap_viscm function. This function wraps the viscm tool to display colormaps. To iterate through and visualize all colormaps in the library, use cmocean.cm.cmapnames to get the names and cmocean.cm.cmap_d to retrieve the actual colormap objects.

    import cmocean
    
    # Iterate through all colormap names and visualize them using viscm
    for cmapname in cmocean.cm.cmapnames:
        cmocean.plots.wrap_viscm(cmocean.cm.cmap_d[cmapname], saveplot=False)
  10. Modify colormaps with cmocean.tools

    main

    The cmocean.tools module provides utilities to manipulate colormap instances:

    • lighten(cmap, alpha): Returns a lighter version of the colormap by applying an alpha value below 1. Useful for overlaying contours.
    • crop_by_percent(cmap, percent, which='both'|'max'|'min', N=None): Crops a portion of the colormap based on percentage.
      • which='both': Removes from both ends.
      • which='max': Removes from the top end.
      • which='min': Removes from the bottom end.
    • crop(cmap, vmin, vmax, N=None): Removes parts of the colormap based on specific data values. This is useful when you want the colorbar to only represent the range of data actually present in your plot.
    • get_dict(cmap, N): Returns a dictionary defining the colormap.
    • cmap(rgb_array, N): Creates a colormap instance from an RGB input array.
    import cmocean
    import cmocean.cm as cmo
    
    # Lighten a colormap
    light_cmap = cmocean.tools.lighten(cmo.matter, 0.5)
    
    # Crop by percentage (e.g., remove 30% from both ends)
    cropped_pct = cmocean.tools.crop_by_percent(cmo.tarn, 30, which='both')
    
    # Crop by specific values
    # Removes values outside the [-50, 200] range
    cropped_val = cmocean.tools.crop(cmo.topo, -50, 200)
  11. Reference of cmocean diverging colormaps

    main

    The following colormaps are designed for diverging datasets, centered around a critical value (usually zero):

    • balance: Dark blue (negative) $\rightarrow$ off-white (zero) $\rightarrow$ dark red (positive). Ideal for sea surface elevation.
    • delta: Darker blues $\rightarrow$ off-white $\rightarrow$ yellow-green. Ideal for diverging velocity values.
    • curl: Darker teal $\rightarrow$ off-white $\rightarrow$ magenta. Ideal for diverging vorticity values.
    • diff: Blues $\rightarrow$ browns. Diverging colormap.
    • tarn: Dry browns $\rightarrow$ greens/blues. Designed as a complementary colormap to rain for representing rain anomalies.