I am trying to create a colorbar where the bounds in the first half is the reciprocal of the second half and is centred on 1. So the bounds should be:
[1/3,1/2.5 1/2, 1/1.75,1/1.5,1/1.25,1,1.25,1.5,1.75,2,2.5,3]
Also, I am trying to insert white into the middle of the colorbar.
What I have tried to do so far is:
import matplotlib.colors as mpc
import matplotlib.pyplot as plt
from pylab import cm
from matplotlib.colors import BoundaryNorm
import numpy as np
upper_levels = np.array([1.25,1.5,1.75,2,2.5,3])
lower_levels = 1/upper_levels # The decimal values are the inverse of these values
lower_levels = np.flip(lower_levels, axis = 0) # Need to order them in the right direction
levels = np.concatenate((lower_levels,np.array([1]),upper_levels))
number_levels = len(levels)
cmap = cm.get_cmap('RdBu', number_levels)(levels)
cmap = np.insert(cmap, int(len(cmap)/2),[1,1,1,1], axis = 0)
cmap = np.insert(cmap, int(len(cmap)/2),[1,1,1,1], axis = 0)
custom_RdBu = mpc.LinearSegmentedColormap.from_list('RdWtBu', cmap, number_levels + 2)
delta = 0.01
x = y = np.arange(0, 3 + delta, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
CS3 = plt.contourf(X,Y,Z, cmap = custom_RdBu)
plt.colorbar(CS3)