My code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
x = y = np.linspace(0, 10, 51)
X, Y = np.meshgrid(x, y)
Z = X+Y # Z.min() => 0, Z.max() => 20
cf = plt.contourf(X, Y, Z,
levels=[5, 10, 15],
norm=colors.BoundaryNorm([5, 10, 15], 256, extend='both'))
cb = plt.colorbar(cf, extend='both')
plt.show()
Its output:
My expectations:
My question:
What have I done wrong?
As @JohanC notes, colorbar does strange things with a contour. However, in this simple case, why are you using BoundaryNorm?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
x = y = np.linspace(0, 10, 51)
X, Y = np.meshgrid(x, y)
Z = X+Y # Z.min() => 0, Z.max() => 20
cf = plt.contourf(X, Y, Z,
levels=[5, 10, 15], extend='both')
cb = plt.colorbar(cf, extend='both')
plt.show()
Does exactly what you want.