I have an uneven colormap and I want the 0 to be white. All negative colors have to be bluish and all positive colors have to be reddish. My current attempt displays the 0 bluish and the 0.7 white.
Is there any way to set the 0 to white?
import numpy as np
import matplotlib.colors as colors
from matplotlib import pyplot as m
bounds_min = np.arange(-2, 0, 0.1)
bounds_max = np.arange(0, 4.1, 0.1)
bounds = np.concatenate((bounds_min, bounds_max), axis=None)
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) # I found this on the internet and thought this would solve my problem. But it doesn't...
m.pcolormesh(xx, yy, interpolated_grid_values, norm=norm, cmap='RdBu_r')
The other answer makes it a little more complicated than it needs to be. In order to have the middle point of the colormap at 0, use a DivergingNorm
with vcenter=0
.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import DivergingNorm
x, y = np.meshgrid(np.linspace(0,50,51), np.linspace(0,50,51))
z = np.linspace(-2,4,50*50).reshape(50,50)
norm = DivergingNorm(vmin=z.min(), vcenter=0, vmax=z.max())
pc = plt.pcolormesh(x,y,z, norm=norm, cmap="RdBu_r")
plt.colorbar(pc)
plt.show()
Note: From matplotlib 3.2 onwards DivergingNorm
will be renamed to TwoSlopeNorm