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.,\
resolution='c',
llcrnrlon=0.,urcrnrlon=357.5)
#-- draw coastlines, state and country boundaries, edge of map
map.drawcoastlines()
#map.drawstates()
#map.drawcountries()
#-- 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')
map.drawmeridians(np.arange(0.,357.5,60.),labels=[0,0,0,1],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)
cmap2.set_over('darkred')
cmap2.set_under('navy')
#-- 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
cbar.ax.tick_params(axis='y',direction='in',labelsize=7)
#-- add plot title
plt.title(title,loc='left',fontdict=font)
#-- displa on screen
#plt.show()
#-- 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:
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.