I have been playing with the from_levels_and_colors
function, so that I can have an extended colorbar on a pcolormesh plot, similar to contourf. Here is my example contourf plot:
import numpy as np
import matplotlib.pyplot as plt
a = np.arange(12)[:,np.newaxis] * np.ones(8)
levels = np.arange(1.5, 10, 2)
plt.contourf(a, cmap='RdYlBu', levels=levels, extend='both')
plt.colorbar()
To produce an analogous pcolormesh plot I need to supply a sequence of colors, so I have:
from matplotlib.colors import from_levels_and_colors
n_colors = len(levels) + 1
cmap = plt.get_cmap('RdYlBu', n_colors)
colors = cmap(range(cmap.N))
cmap, norm = from_levels_and_colors(levels, colors, extend='both')
plt.pcolormesh(a, cmap=cmap, norm=norm)
plt.colorbar()
The middle four colors in the pcolormesh are lighter than in the contourf. How might I choose them so they match?