cmocean package

Submodules

cmocean.cmocean module

cmocean.plots module

Plots with colormaps.

Make plot of colormaps and labels, like in the matplotlib gallery.

Parameters:saveplot=False – Whether to save the plot or not.
cmocean.plots.plot_lightness(saveplot=False)

Plot lightness of colormaps together.

cmocean.plots.quick_plot(cmap, fname=None, fig=None, ax=None, N=10)

Show quick test of a colormap.

cmocean.plots.test(cmap, fig=None, ax=None)

Test colormap by plotting.

Parameters:cmap – A colormap instance. Use a named one with cm.get_cmap(colormap)
cmocean.plots.wrap_viscm(cmap, dpi=100, saveplot=False)

Evaluate goodness of colormap using perceptual deltas.

Parameters:
  • cmap – Colormap instance.
  • dpi=100 – dpi for saved image.
  • saveplot=False – Whether to save the plot or not.

cmocean.tools module

Plot up stuff with colormaps.

cmocean.tools.cmap(rgbin, N=256)

Input an array of rgb values to generate a colormap.

Parameters:
  • rgbin – An [mx3] array, where m is the number of input color triplets which are interpolated between to make the colormap that is returned. hex values can be input instead, as [mx1] in single quotes with a #.
  • N=10 – The number of levels to be interpolated to.
cmocean.tools.crop(cmapin, vmin, vmax, pivot, N=None, dmax=None)

Crop end or ends of a diverging colormap by vmin/vmax values.

Parameters:
  • cmap – A colormap object, like cmocean.cm.matter.
  • vmin/vmax – vmin/vmax for use in plot with colormap.
  • pivot – center point to be used in plot with diverging colormap.
  • N=None – User can specify the number of rows for the outgoing colormap. If unspecified, N from incoming colormap will be used and values will be interpolated as needed to fill in rows.
  • dmax=None – dmax is the highest number to be included in a plot with the colormap; values higher in magnitude than dmax are removed from both ends of colormap. It should be less than abs(vmin) and abs(vmax), which should be equal for this parameter to be used.

Outputs resultant colormap object.

This function can be used for sequential and other non-diverging colormaps
but it is easier to use that way through crop_by_percent().
This should be useful for plotting bathymetry and topography data with the
topo colormap when max bathymetry value is different from max topography.
Example usage:

# example for crop on min end of diverging colormap vmin = -2; vmax = 5; pivot = 0 newcmap = crop(cmocean.cm.curl, vmin, vmax, pivot) A = np.random.randint(vmin, vmax, (5,5)) plt.pcolormesh(A, vmin=vmin, vmax=vmax, cmap=newcmap) plt.colorbar()

# example for crop on max end of diverging colormap vmin = -10; vmax = 8; pivot = 0 newcmap = crop(cmocean.cm.delta, vmin, vmax, pivot) A = np.random.randint(vmin, vmax, (5,5)) plt.pcolormesh(A, vmin=vmin, vmax=vmax, cmap=newcmap) plt.colorbar()

cmocean.tools.crop_by_percent(cmap, per, which='both', N=None)

Crop end or ends of a colormap by per percent.

Parameters:
  • cmap – A colormap object, like cmocean.cm.matter.
  • per – Percent of colormap to remove. If which==’both’, take this percent off both ends of colormap. If which==’min’ or which==’max’, take percent only off the specified end of colormap.
  • which='both' – which end or ends of colormap to cut off. which=’both’ removes from both ends, which=’min’ from bottom end, and which=’max’ from top end.
  • N=None – User can specify the number of rows for the outgoing colormap. If unspecified, N from incoming colormap will be used and values will be interpolated as needed to fill in rows.

Outputs resultant colormap object.

This is a wrapper around crop() to make it easier to use for cropping
based on percent.
Examples:

# example with oxy map: cut off yellow part which is top 20% # compare with full colormap vmin = 0; vmax = 10; pivot = 5 A = np.random.randint(vmin, vmax, (5,5)) fig, axes = plt.subplots(1, 2) mappable = axes[0].pcolormesh(A, vmin=vmin, vmax=vmax, cmap=cmocean.cm.oxy) fig.colorbar(mappable, ax=axes[0]) vmin = 0; vmax = 8; pivot = 5 newcmap = crop_by_percent(cmocean.cm.oxy, 20, which=’max’, N=None) plt.figure() plt.pcolormesh(A, vmin=vmin, vmax=vmax, cmap=newcmap) plt.colorbar()

# example with oxy map: cut off red part which is bottom 20% # compare with full colormap vmin = 0; vmax = 10; pivot = 5 A = np.random.randint(vmin, vmax, (5,5)) fig, axes = plt.subplots(1, 2) mappable = axes[0].pcolormesh(A, vmin=vmin, vmax=vmax, cmap=cmocean.cm.oxy) fig.colorbar(mappable, ax=axes[0]) vmin = 2; vmax = 10; pivot = 5 A = np.random.randint(vmin, vmax, (5,5)) newcmap = crop_by_percent(cmocean.cm.oxy, 20, which=’min’, N=None) plt.figure() plt.pcolormesh(A, vmin=vmin, vmax=vmax, cmap=newcmap) plt.colorbar()

# crop both dark ends off colormap to reduce range newcmap = crop_by_percent(cmocean.cm.balance, 10, which=’both’, N=None) plt.figure() A = np.random.randint(-5, 5, (5,5)) plt.pcolormesh(A, vmin=vmin, vmax=vmax, cmap=newcmap) plt.colorbar()

cmocean.tools.get_dict(cmap, N=256)

Change from rgb to dictionary that LinearSegmentedColormap expects. Code from https://mycarta.wordpress.com/2014/04/25/convert-color-palettes-to-python-matplotlib-colormaps/ and http://nbviewer.ipython.org/github/kwinkunks/notebooks/blob/master/Matteo_colourmaps.ipynb

cmocean.tools.lighten(cmapin, alpha)

Lighten a colormap by adding alpha < 1.

Parameters:
  • cmap – A colormap object, like cmocean.cm.matter.
  • alpha – An alpha or transparency value to assign the colormap. Alpha of 1 is opaque and of 1 is fully transparent.

Outputs resultant colormap object.

This will lighten the appearance of a plot you make using the output
colormap object. It is also possible to lighten many plots in the plotting function itself (e.g. pcolormesh or contourf).
cmocean.tools.print_colormaps(cmaps, N=256, returnrgb=True, savefiles=False)

Print colormaps in 256 RGB colors to text files.

Parameters:returnrgb=False – Whether or not to return the rgb array. Only makes sense to do if print one colormaps’ rgb.

Module contents

cmocean is a package to help standardize colormaps for commonly-plotted oceanographic properties.

See README.md for an overview on instructions.