Is it an issue that map.colorbar(extend=’both’) does not work?

I made a contour plot over a global map.
Since it is a contour plot, I also plot a colorbar along with it.

Bug report

When I set map.colorbar(…, extend=’both’), it does not work at all. The colorbar has no changes.
Can anyone help?

Thank you very much!

Bug summary

Code for reproduction

def plot_ann_mean_precip(lat,lon,var, unit, title, rngeup, rngedo, interval, colorbarr,font):
    lat  = lat  
    lon  = lon 
    var  = var         

    #-- create figure and axes instances
    #dpi = 100
    #fig = plt.figure(figsize=(1000/dpi, 1000/dpi), dpi=dpi)
    #ax  = fig.add_axes([0.1,0.1,0.8,0.9])

    #-- create map
    map = Basemap(projection='cyl',
                  llcrnrlat= -90.,urcrnrlat= 90.,\

    #-- draw coastlines, state and country boundaries, edge of map

    #-- create and draw meridians and parallels grid lines
    map.drawparallels(np.arange( -90., 100.,30.),labels=[1,0,0,0],fontsize=8,linewidth=0.4,color='white')

    #-- convert latitude/longitude values to plot x/y values
    x, y = map(*np.meshgrid(lon,lat))

    #-- contour levels
    clevs = np.arange(rngedo,rngeup,interval)
    cmap2 = plt.get_cmap(colorbarr)

    #-- draw filled contours
    cnplot = map.contourf(x,y,var,clevs,cmap=cmap2)

    #-- add colorbar
    cbar = map.colorbar(cnplot, location='right', pad=0.05, boundaries=[clevs[0]-100] + clevs + [clevs[-1]+100], extend='both')      #-- pad: distance between map and colorbar
    cbar.set_label(unit,size=7)                                                                            #-- add colorbar title string'y',direction='in',labelsize=7)

    #-- add plot title

    #-- displa on screen

    #-- maximize and save PNG file
    #plt.savefig('plot_matplotlib_contour_filled_rect.png', bbox_inches='tight', dpi=dpi)

Actual outcome

# If applicable, paste the console output here

Expected outcome

Matplotlib version

  • Operating system:
  • Matplotlib version:
  • Matplotlib backend (print(matplotlib.get_backend())):
  • Python version:
  • Jupyter version (if applicable):
  • Other libraries:

1 possible answer(s) on “Is it an issue that map.colorbar(extend=’both’) does not work?

  1. See #16051 (and links there in) for work related to this. The issue is that the Artist (in this case your contour) overrides what ever input you provide when you create the colorbar. In the mpl33 this will warn you. Both now and in the future you will need to pass the extend kwarg when you create the contour for it to propagate to the color bar.