public
Authored by avatar Franco Alberto Cardillo

Python, select range from colormap

Select only a range from a colormap, for example when you want to exclude whites.

Source: https://stackoverflow.com/questions/64633018/removing-white-or-light-colors-from-matplotlib-color-palette

Edited
colormapRange.py 651 bytes
#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()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment