Search code examples
pythonpython-3.xmatplotlibcolorbarcontourf

Crop extended marker in colorbar


I had to use extend='min' in using contourf to include coloring the values below my given range (negative values). I'd like to ask if how do I crop this extended marker (pointed by the arrow)

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5,5))
ax = plt.contourf(x, y, z, cmap='Spectral', vmin=0, vmax=60, norm=norm, extend='min')

# colorbar
cax = fig.add_axes([.918, 0.175, 0.045, 0.6])
cb = fig.colorbar(ax, cax=cax)

enter image description here

and make it to something like this

enter image description here

Thank you


Solution

  • You could use this code:

    bounds = [5*i for i in range(13)]
    cax = fig.add_axes([.918, 0.175, 0.045, 0.6])
    fig.colorbar(mpl.cm.ScalarMappable(cmap = 'Spectral', norm = norm),
                 cax = cax,
                 ticks = bounds,
                 boundaries = [0] + bounds[1:-1] + [60])
    

    enter image description here