cmocean: Colormaps for Oceanography
cmocean provides a collection of aesthetically pleasing and perceptually uniform colormaps specifically designed for oceanographic data visualization. It integrates seamlessly with Matplotlib. The current version is 4.0.3, and it receives active maintenance with frequent patch releases and less frequent major version updates.
Common errors
-
AttributeError: module 'cmocean' has no attribute 'haline'
cause Attempting to access a colormap directly from the top-level `cmocean` module instead of its `cm` submodule.fixColormaps are stored in `cmocean.cm`. Use `cmocean.cm.haline` (or `cm.haline` if aliased `import cmocean.cm as cm`). -
TypeError: 'NoneType' object is not callable (or similar error when trying to show a plot)
cause `matplotlib.pyplot` was imported, but a plotting function (like `plt.show()`) was called in an environment without a suitable graphical backend or `matplotlib` is not correctly installed.fixEnsure `matplotlib` is installed (`pip install matplotlib`). If running a script without an interactive display, use `plt.savefig('my_plot.png')` instead of `plt.show()` or configure a non-interactive backend like `matplotlib.use('Agg')`. -
KeyError: 'cmap_name' (when using cmocean.tools.get_cmap)
cause In `cmocean` versions prior to `4.0.2`, a typo in the `cmocean.tools` module prevented some colormaps from being correctly identified by the utility functions.fixUpgrade `cmocean` to version `4.0.2` or later: `pip install --upgrade cmocean`.
Warnings
- gotcha cmocean colormaps are typically accessed through the `cmocean.cm` submodule. Attempting to directly access colormaps from the top-level `cmocean` module (e.g., `cmocean.haline`) will result in an `AttributeError`.
- gotcha While `cmocean` provides colormaps, its practical utility relies entirely on `matplotlib` for rendering. If `matplotlib` is not installed or configured correctly, `cmocean` colormaps cannot be displayed or used effectively.
- gotcha In `cmocean` versions prior to `4.0.2`, a typo in the `cmocean.tools` module could cause issues when trying to retrieve certain colormaps using utility functions like `cmocean.tools.get_cmap()`. Direct access via `cmocean.cm.colormap_name` was unaffected.
Install
-
pip install cmocean
Imports
- cm
import cmocean as cm
import cmocean.cm as cm
Quickstart
import matplotlib.pyplot as plt
import cmocean.cm as cm
import numpy as np
# Create some dummy data
data = np.random.rand(10, 10)
# Plot using a cmocean colormap
plt.imshow(data, cmap=cm.haline)
plt.colorbar(label='Data Value')
plt.title('Data with cmocean.haline Colormap')
# To display the plot, uncomment the line below in an interactive environment
# plt.show()
# Save to a file if not in an interactive environment
# plt.savefig('cmocean_example.png')
plt.close() # Close plot to prevent display if running as script