I have an uneven color ramp 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 attempted displays the 0 bluish and the 0.7 white.
Is there anyway 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
withvcenter=0
.Note: From matplotlib 3.2 onwards
DivergingNorm
will be renamed toTwoSlopeNorm