I am plotting an animated contourf map in matplotlib with a colorbar that changes at each frame. I want to keep the colorbar centered at zero (I am using a diverging colormap) and to do so I use an odd number of levels. The problem is, when I do this, even though the central color of the colormap (cm.seismic) is white, this color does not appear in the colormap. I want to be able to replace the color of the smallest values (the light red and the light blue) by white, so that instead of having one central level whose color is white (zero), I have two (two smallest values).
CodePudding user response:
Instead of providing a colormap, you can provide a list of colors. That list can be calculated from the given colormap, and the middle colors can be set explicitly to white.
Here is an example:
import matplotlib.pyplot as plt
import numpy as np
x = y = np.linspace(-3.0, 3.01, 100)
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
num_levels = 9
colors = plt.cm.seismic(np.linspace(0, 1, num_levels 1))
colors[num_levels // 2] = [1, 1, 1, 1] # set to white
colors[num_levels // 2 1] = [1, 1, 1, 1]
fig, ax1 = plt.subplots(figsize=(10, 5))
CS = ax1.contourf(X, Y, Z, num_levels, colors=colors, origin='lower')
cbar = fig.colorbar(CS, ax=ax1)
plt.show()