How do I make my colour bar for Cartopy have a specific range set by me?

6.3k views Asked by At

I am comparing various missions and I want the colour bar to have a max and min set by me. I am not sure how to do this, any help with this? The missions themselves will keep within a range but I want to set it, so it is easily comparable. Some missions are different and some arent so I dont want the computer to set the max and min automatically.

enter image description here

crs_latlon = ccrs.PlateCarree()
def make_plot(projection_name, projection_crs):

    ax = plt.axes(projection=projection_crs)
    #ax.set_extent((-65.0, -58, 40, 47.7), crs=crs_latlon)
    ax.set_extent((-65.0, -62, 43, 45.5), crs=crs_latlon)

    #Add coastlines and meridians/parallels (Cartopy-specific).
    plt.gca().coastlines('10m')

    ax.gridlines(crs=crs_latlon, linestyle='-')
    # Add a title, legend, and display.
    ax.set_title(''.join(("Mission #13: Attenuation Coeffiecient - ",
                      projection_name)))

    cb = plt.scatter(avglonlist, avglatlist, c=klist, cmap=coolwarm)
    plt.colorbar(cb, cmap=coolwarm, orientation='vertical',ticklocation='auto')
    #plt.colorbar.ColorbarBase(ax=ax, cmap = coolwarm, orientation='vertical', ticklocation='auto',
     #                        norm=plt.colors.Normalize(vmin=0, vmax=1))
    iplt.show()

def main():
# Demonstrate with two different display projections.
    make_plot('Equidistant Cylindrical', ccrs.PlateCarree())
    graph_my_latandk(avglatlist,klist)

if __name__ == '__main__':
    main()
2

There are 2 answers

0
Serenity On BEST ANSWER

I guess you have to create your own color bar like here:

import matplotlib.pylab as plt
from matplotlib import colorbar, colors

fig = plt.figure(figsize=(8, 3))
ax = fig.add_axes([.05, .05, .05, .7]) # position of colorbar
cbar = colorbar.ColorbarBase(ax, cmap=plt.get_cmap('coolwarm'),
  norm=colors.Normalize(vmin=-.5, vmax=1.5)) # set min, max of colorbar
cbar.set_clim(-.5, .5) # set limits of color map
plt.show()

vmin, vmax allow to set limits of colorbar but clim allow to set limits of color map.

0
Rob On

If you pass vmin and vmax to scatter, you can set the colour range of the scatter plot and the colourbar will bet set accordingly.

e.g.

cb = plt.scatter(avglonlist, avglatlist, c=klist, cmap=coolwarm, vmin=-4, vmax=2)
plt.colorbar(cb, cmap=coolwarm, orientation='vertical',ticklocation='auto')