Altering Basemap Colobar and Label positioning

Hi all,

I am having great difficulty understanding how to change the size of my basemap colorbar, altering its position and moving the text label all at the same time. I would like to:

Shrink the size of the colorbar (there doesn’t seem to be a shrink property in the basemap.colorbar() method (only plt.colorbar() or fig.colorbar())

Move the bar so it is not centered but instead so its right edge is aligned vertically with the right end of the basemap.

Move the colorbar W/m^2 text label so it is not below the colorbar but is instead directly to its left.

I looked up several other responses online that mentioned doing things such as adding a second axes, or using the shrink command from plt.colorbar(), and changing some other properties such as padding, but in the end, most of these alterations
seem to introduce another problem when I try them. Even after viewing their documentation, I still do not fully understand their proper usage. Also, I tried a few properties listed in the matplotlib documentation such as anchor and panchor in my the fig.colorbar()
method in attempt to move the bar around but when I tried to run it, the keyword was not recognized by the interpreter and produced an error (it seems strange that some of the keywords listed in the docs aren’t being recognized; and I’m pretty sure I have
the most current matplotlib version too). You can see some of the commented commands I tried in the code below (not all at once, of course, but just in various conjunctions with one another). Here is an example of my code and an attached example of what the
plot currently looks like after running said code. Any helpful advice would be greatly appreciated. So confused right now and I feel like I’ve read the docs over and over to little avail (P.S. Getting down to the nitty gritty of working with matplotlib objects
and understanding its inner workings to customize my plots better is really confusing, even with the docs, (sigh)):

swi = swi.reshape(1059, 1799)

lat = lat.reshape(1059, 1799)

lon = lon.reshape(1059, 1799)

def plot_conus():

m = mpl_toolkits.basemap.Basemap(

llcrnrlon=-135.0,

llcrnrlat=19.0,

urcrnrlon=-60.0,

urcrnrlat=54.0,

projection=‘mill’,

resolution=‘c’)

m.drawcoastlines()

m.drawcountries()

m.drawstates()

draw parallels

parallels = np.arange(0.,90,10.)

m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)

draw meridians

meridians = np.arange(180.,360.,10.)

m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

return m

find hex color values at

http://www.colorpicker.com

swi_colors = [

#"#f800fd", # light purple

#"#9854c6", # dark purple

#04e9e7”,

#019ff4”,

#0300f4”,

#02fd02”,

#01c501”,

#008e00”,

#fdf802”,

#e5bc00”,

#fd9500”,

#fd0000”,

#d40000”,

#bc0000”,

#A10505” # brick

]

swi_colormap = matplotlib.colors.ListedColormap(swi_colors)

m = plot_conus()

levels = []

for i in range(13):

levels.append(i*90.0)

create black and white cross at observatory location on map

site_lon = -87.99495

site_lat = 41.70121

x_site, y_site = m(site_lon, site_lat)

m.plot(x_site, y_site, ‘w+’, markersize=30, markeredgewidth=8) # white cross

m.plot(x_site, y_site, ‘k+’, markersize=25, markeredgewidth=3) # black cross

norm = matplotlib.colors.BoundaryNorm(levels, 13)

cax = m.pcolormesh(lon, lat, swi, latlon=True, norm=norm,

cmap=swi_colormap)

#cbar = m.colorbar(cax)

fig = plt.gcf()

#ax = plt.gca()

#cbar = fig.colorbar(cax, orientation=‘horizontal’, shrink=0.75)

#cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8])

#cb = fig.colorbar(cax)

cbar = m.colorbar(cax, location=‘bottom’, pad=‘6%’)

cbar.set_label(’$W/m^2$’, fontsize=18)

plt.title('NOAA LAPS GHI, RT ’ + modelrun_time_label + ', VT ’ + fcst_time_label)

plt.show()

ghi.gif

Hi Andruska,

The Basemap.colorbar has a “size” keyword to allow you have the shrink-like function to adjust the size of the colorbar.

Otherwise you can creat an axes on the exact position you want to hold the colorbar, like below I have prepared an example for you:

arr = np.arange(100).reshape(10,10)
fig,ax = plt.subplots(1,1)
cs = ax.imshow(arr)
ax.set_position([0.2, 0.3, 0.6, 0.6])
axt = fig.add_axes([0.4,0.2,0.4,0.05])
cbar = plt.colorbar(cs,cax=axt,orientation=‘horizontal’)

fig.text(0.25,0.22,‘I am label’,va=‘center’,size=13)
draw()

I think it’s hard to use the colorbar.set_label put the label directly on the left of your colorbar, I rather suggest you to use fig.text to

position exactly a text for your label.

At the beginning of matplotlib you might feel confused, but after investing a significant amount of time you feel it extremely flexible, and going to like it :slight_smile:

Cheers,

Chao

···

On Mon, Jun 16, 2014 at 6:32 PM, Andruska, Michael [via matplotlib] <[hidden email]> wrote:

Hi all,

I am having great difficulty understanding how to change the size of my basemap colorbar, altering its position and moving the text label all at the same time. I would like to:

Shrink the size of the colorbar (there doesn’t seem to be a shrink property in the basemap.colorbar() method (only plt.colorbar() or fig.colorbar())

Move the bar so it is not centered but instead so its right edge is aligned vertically with the right end of the basemap.

Move the colorbar W/m^2 text label so it is not below the colorbar but is instead directly to its left.

I looked up several other responses online that mentioned doing things such as adding a second axes, or using the shrink command from plt.colorbar(), and changing some other properties such as padding, but in the end, most of these alterations
seem to introduce another problem when I try them. Even after viewing their documentation, I still do not fully understand their proper usage. Also, I tried a few properties listed in the matplotlib documentation such as anchor and panchor in my the fig.colorbar()
method in attempt to move the bar around but when I tried to run it, the keyword was not recognized by the interpreter and produced an error (it seems strange that some of the keywords listed in the docs aren’t being recognized; and I’m pretty sure I have
the most current matplotlib version too). You can see some of the commented commands I tried in the code below (not all at once, of course, but just in various conjunctions with one another). Here is an example of my code and an attached example of what the
plot currently looks like after running said code. Any helpful advice would be greatly appreciated. So confused right now and I feel like I’ve read the docs over and over to little avail (P.S. Getting down to the nitty gritty of working with matplotlib objects
and understanding its inner workings to customize my plots better is really confusing, even with the docs, (sigh)):

swi = swi.reshape(1059, 1799)

lat = lat.reshape(1059, 1799)

lon = lon.reshape(1059, 1799)

def plot_conus():

m = mpl_toolkits.basemap.Basemap(

llcrnrlon=-135.0,

llcrnrlat=19.0,

urcrnrlon=-60.0,

urcrnrlat=54.0,

projection=‘mill’,

resolution=‘c’)

m.drawcoastlines()

m.drawcountries()

m.drawstates()

draw parallels

parallels = np.arange(0.,90,10.)

m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10)

draw meridians

meridians = np.arange(180.,360.,10.)

m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10)

return m

find hex color values at

http://www.colorpicker.com

swi_colors = [

#“#f800fd”, # light purple

#“#9854c6”, # dark purple

#04e9e7”,

#019ff4”,

#0300f4”,

#02fd02”,

#01c501”,

#008e00”,

#fdf802”,

#e5bc00”,

#fd9500”,

#fd0000”,

#d40000”,

#bc0000”,

#A10505” # brick

]

swi_colormap = matplotlib.colors.ListedColormap(swi_colors)

m = plot_conus()

levels =

for i in range(13):

levels.append(i*90.0)

create black and white cross at observatory location on map

site_lon = -87.99495

site_lat = 41.70121

x_site, y_site = m(site_lon, site_lat)

m.plot(x_site, y_site, ‘w+’, markersize=30, markeredgewidth=8) # white cross

m.plot(x_site, y_site, ‘k+’, markersize=25, markeredgewidth=3) # black cross

norm = matplotlib.colors.BoundaryNorm(levels, 13)

cax = m.pcolormesh(lon, lat, swi, latlon=True, norm=norm,

cmap=swi_colormap)

#cbar = m.colorbar(cax)

fig = plt.gcf()

#ax = plt.gca()

#cbar = fig.colorbar(cax, orientation=‘horizontal’, shrink=0.75)

#cbaxes = fig.add_axes([0.8, 0.1, 0.03, 0.8])

#cb = fig.colorbar(cax)

cbar = m.colorbar(cax, location=‘bottom’, pad=‘6%’)

cbar.set_label(‘W/m^2’, fontsize=18)

plt.title('NOAA LAPS GHI, RT ’ + modelrun_time_label + ', VT ’ + fcst_time_label)

plt.show()


HPCC Systems Open Source Big Data Platform from LexisNexis Risk Solutions

Find What Matters Most in Your Big Data with HPCC Systems

Open Source. Fast. Scalable. Simple. Ideal for Dirty Data.

Leverages Graph Analysis for Fast Processing & Easy Data Exploration

http://p.sf.net/sfu/hpccsystems


Matplotlib-users mailing list

[hidden email]

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

ghi.gif (104K) Download Attachment


If you reply to this email, your message will be added to the discussion below:

http://matplotlib.1069221.n5.nabble.com/Altering-Basemap-Colobar-and-Label-positioning-tp43534.html

To start a new topic under matplotlib - users, email [hidden email]

  To unsubscribe from matplotlib, click here.


  [NAML](http://matplotlib.1069221.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml)


please visit:
http://www.globalcarbonatlas.org/


Chao YUE
Laboratoire des Sciences du Climat et de l’Environnement (LSCE-IPSL)
UMR 1572 CEA-CNRS-UVSQ
Batiment 712 - Pe 119
91191 GIF Sur YVETTE Cedex

Tel: (33) 01 69 08 29 02; Fax:01.69.08.77.16