The solution will certainly not create this level from the start.
Here we select a maximum of 5 levels according to the locator and delete the lowest one when calling the contourf chart, so this level does not exist at all. Then the automatic creation of the color panel works flawlessly.
import numpy as np; np.random.seed(5) import matplotlib.pyplot as plt from matplotlib import ticker from scipy import stats x = np.random.normal(3, 1, 100) y = np.random.normal(0, 2, 100) X, Y = np.mgrid[x.min():x.max():100j, y.min():y.max():100j] positions = np.vstack([X.ravel(),Y.ravel()]) values = np.vstack([x,y]) kernel = stats.gaussian_kde(values) Z = np.reshape(kernel(positions).T, X.shape) N=4 locator = ticker.MaxNLocator(N + 1, min_n_ticks=N) lev = locator.tick_values(Z.min(), Z.max()) fig, ax = plt.subplots() c = ax.contourf(X,Y,Z,levels=lev[1:]) ax.scatter(x,y, s=9, c="k") fig.colorbar(c) plt.show()

source share