mplutils.crop_colormap#

mplutils.crop_colormap(cmap, x=0.0, y=1.0, new_cmap_name=None, register=False, n_lut=256)[source]#

Return a colormap within (x,y) range

Parameters:
cmap

A matplotlib colormap, as returned by matplotlib.colormaps["colormap_name"].

xfloat, default = 0.0

Lower limit of the colorbar (in relative units).

yfloat, default = 1.0

Upper limit of the colorbar (in relative units).

new_cmap_namestr, optional

Optionally, give the colormap a name.

If None, colormap will be called "_new_colormap".

registerbool, default False

Register the colormap.

If it is registered, one can call it simply by it’s name (i.e., new_cmap_name).

n_lutint, default 256

The number of segments of the old colormap.

Returns:
new_cmapmatplotlib.colors.LinearSegmentedColormap.

The new colormap.

Examples

import matplotlib.pyplot as plt
import mplutils as mplu

viridis = plt.cm.get_cmap("viridis")
viridis_cropped = mplu.crop_colormap(viridis, 0.3, 0.7)
cmaps = viridis, viridis_cropped

fig, axs = plt.subplots(2, 1, layout=mplu.FixedLayoutEngine())

titles = "default colormap (viridis)", "cropped colormap (viridis)"
for ax, title, cmap in zip(axs, titles, cmaps):
    fig.colorbar(plt.cm.ScalarMappable(cmap=cmap), cax=ax, orientation="horizontal")
    ax.set_title(title)
    mplu.set_axes_size(7, 1.0, ax=ax)

(Source code, png, hires.png, pdf)

../../_images/crop_colormap.png