Remove the lowest color from the colorbar in Seaborn / Matplotlib

If I set shade_lowest = False , the color bar still contains the lowest level (purple-ish). Is there a general way to completely remove it?

enter image description here

 import seaborn as sns import numpy as np import matplotlib.pyplot as plt a = np.random.normal(0, 1, 100) b = np.random.normal(0, 1, 100) fig, ax = plt.subplots() sns.kdeplot(a, b, shade = True, shade_lowest = False, cmap = "viridis", cbar = True, n_levels = 4, ax = ax) plt.show() 
+5
source share
1 answer

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() 

enter image description here

+3
source

Source: https://habr.com/ru/post/1275822/


All Articles