Skip to content Skip to sidebar Skip to footer

Custom Colormap

I want to plot a heatmap with a custom colormap similar to this one, although not exactly. I'd like to have a colormap that goes like this. In the interval [-0.6, 0.6] the color i

Solution 1:

Colormaps are normalized in the 0..1 range. So if your data limits are -1..1, -0.6 would be normalized to 0.2, +0.6 would be normalized to 0.8.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors

norm = matplotlib.colors.Normalize(-1,1)
colors = [[norm(-1.0), "darkblue"],
          [norm(-0.6), "lightgrey"],
          [norm( 0.6), "lightgrey"],
          [norm( 1.0), "red"]]

cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", colors)


fig, ax=plt.subplots()
x = np.arange(10)
y = np.linspace(-1,1,10)
sc = ax.scatter(x,y, c=y, norm=norm, cmap=cmap)
fig.colorbar(sc, orientation="horizontal")
plt.show()

enter image description here

Post a Comment for "Custom Colormap"