I have the following data frame:
import pandas as pd
Index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
Cols = ['A', 'B', 'C', 'D']
data= [[ 1, 0.3, 2.1, 1.3],[ 2.5, 1, 1, 0.77],[ 0.0, 1, 2, 1],[ 0, 3.2, 1, 1.2],[ 10, 1, 1, 1]]
df = pd.DataFrame(data, index=Index, columns=Cols)
That looks like this:
In [25]: df
Out[25]:
A B C D
aaa 1.0 0.3 2.1 1.30
bbb 2.5 1.0 1.0 0.77
ccc 0.0 1.0 2.0 1.00
ddd 0.0 3.2 1.0 1.20
eee 10.0 1.0 1.0 1.00
What I want to do is to create a heat map with the following condition:
- Value < 1 : Blue
- Value == 1 : White
- 1 < Value < 2: Light Red
- Value >=2 : Dark red
Ideally the color would have to be in gradation. This is my failed poor attempt:
from matplotlib import colors
cmap = colors.ListedColormap(['darkblue','blue','white','pink','red'])
bounds=[-0.5, 0.5, 1.5, 2.5, 3.5]
norm = colors.BoundaryNorm(bounds, cmap.N)
heatmap = plt.pcolor(np.array(data), cmap=cmap, norm=norm)
plt.colorbar(heatmap, ticks=[0, 1, 2, 3])
Which produce this plot:

What's the right way to do it?

matplotlib.colors.LinearSegmentedColormap.from_list.