public
Authored by
Franco Alberto Cardillo

Python, select range from colormap
Select only a range from a colormap, for example when you want to exclude whites.
#You can easily create a custom colormap using LinearSegmentedColormap and choosing the colors that you want (in this case, a subset of the original colormap)
min_val, max_val = 0.3,1.0
n = 10
orig_cmap = plt.cm.Blues
colors = orig_cmap(np.linspace(min_val, max_val, n))
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("mycmap", colors)
demo
gradient = np.linspace(0, 1, 256)
gradient = np.vstack((gradient, gradient))
fig, (ax1, ax2) = plt.subplots(2,1, figsize=(5,2))
ax1.imshow(gradient, cmap=orig_cmap, aspect='auto')
ax1.set_title('original')
ax2.imshow(gradient, cmap=cmap, aspect='auto')
ax2.set_title('custom')
plt.tight_layout()
Please register or sign in to comment