import matplotlib.pyplot as plt
%matplotlib inline
Create colormap from a list of colors
from matplotlib.colors import LinearSegmentedColormap
= LinearSegmentedColormap.from_list(name="my_cmap", colors=["green", "black", "purple"])
custom_cmap custom_cmap
my_cmap
under
bad
over
from matplotlib.colors import ListedColormap, BoundaryNorm
= ListedColormap(name="another_cmap", colors=['green', 'black', 'purple'])
custom_cmap2
custom_cmap2
another_cmap
under
bad
over
# ListedColormap with norm:
= BoundaryNorm([-1, -0.15, 0.15, 1], custom_cmap2.N)
norm2 # BoundaryNorm maps values to INTEGERS instead of floats 0..1
print(norm2(0.5))
= plt.subplots()
f, ax = ax.imshow([[-0.5, -0.1], [0,0.1], [0.8,0.5]], cmap=custom_cmap2, norm=norm2)
p =p, spacing='proportional') f.colorbar(mappable
2
<matplotlib.colorbar.Colorbar at 0x1d1859349d0>
Merge 2 colormaps
= LinearSegmentedColormap.from_list(name="cmap1", colors=["green", "black", "purple"])
cmap1 = LinearSegmentedColormap.from_list(name="cmap2", colors=["blue", "yellow", "orange"]) cmap2
With sharp edge:
import numpy as np
= cmap1(np.linspace(0, 1, 100))
colors_cmap1 = cmap2(np.linspace(0, 1, 200))
colors_cmap2 = np.vstack((colors_cmap1, colors_cmap2))
all_colors
= LinearSegmentedColormap.from_list('merged',all_colors)
cmap_merged
cmap_merged
merged
under
bad
over
With smooth transition:
= cmap1(np.linspace(0, 1, 3))
colors_cmap1 = cmap2(np.linspace(0, 1, 6))
colors_cmap2 = np.vstack((colors_cmap1, colors_cmap2))
all_colors
= LinearSegmentedColormap.from_list('merged',all_colors)
cmap_merged
cmap_merged
merged
under
bad
over