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 is light grey. Above 0.6, the color red intensifies. Below -0.6 another color, say blue, intensifies.
How can I create such a colormap using python and matplotlib?
What I have so far:
In seaborn
there is the command seaborn.diverging_palette(220, 10, as_cmap=True)
which produces a colormap going from blue-light grey-red. But there is still no gap from [-0.6, 0.6].
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With