Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
linlintamu opened this issue Jan 15, 2020 · 4 comments
Closed

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

linlintamu opened this issue Jan 15, 2020 · 4 comments
Labels
Community support Users in need of help.

Comments

@linlintamu
Copy link

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:
@tacaswell
Copy link
Member

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.

@linlintamu
Copy link
Author

Thank you!

The only change I made is map.contourf(..., extend='both') and it works now.

@anntzer anntzer closed this as completed Jan 16, 2020
@anntzer anntzer added the Community support Users in need of help. label Jan 16, 2020
@smowlavi
Copy link

smowlavi commented Jan 6, 2021

This approach does not work when used on a 3D plot with Axes3d.contourf. Passing the kwarg extend='both' to the latter function leads to the error:
AttributeError: 'PathCollection' object has no attribute 'do_3d_projection'

@jklymak
Copy link
Member

jklymak commented Jan 6, 2021

Can you open a new issue. Its not entirely clear what you are doing to get that error. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community support Users in need of help.
Projects
None yet
Development

No branches or pull requests

5 participants