'create norm to white colour in self made cmap

Based on the helpfull instructions here I have made my own cmap, however I am not capable of making sure the white colour is where the norm is zero. There is another answered question that tries to do that, but that code is not longer working. I think it might be to old, perhaps? I am not skilled enough to fix that and apply the norm in my cmap. Would anybody be able to help me out?

Thanks!

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

data = np.linspace(-10,5,20).reshape(5,4)

cmap = {name:plt.get_cmap(name) for name in ('RdBu_r', 'hot')}
N = 50

vmin, vmax = (np.nextafter(min(data.min(), -1), -np.inf), 
              np.nextafter(max(data.max(), 1), np.inf))              # 1

levels = np.concatenate([np.linspace(vmin, 0, N, endpoint=False),
                         np.linspace(0, vmax, N+1, endpoint=True)])  # 2
colors = np.concatenate([cmap[name](np.linspace(0, 1, N)) 
                         for name in ('RdBu_r', 'hot')])           # 3
test = colors[:45]
test1 = colors[62:85]
cresults = np.concatenate((test,test1),axis=0) 

test = levels[:45]
test1 = levels[62:86]
lresults = np.concatenate((test,test1),axis=0) 

cmap, norm = mcolors.from_levels_and_colors(lresults, cresults)

plt.imshow(data, cmap=cmap,
           norm=norm, 
           interpolation='nearest')
bar = plt.colorbar()
plt.show()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source