Skip to content

Commit 442f095

Browse files
author
Jeff Whitaker
committed
use Basemap colorbar method.
1 parent 030a931 commit 442f095

File tree

10 files changed

+42
-113
lines changed

10 files changed

+42
-113
lines changed

examples/barb_demo.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@
4141
# plot barbs.
4242
m.barbs(xv,yv,udat,vdat,length=6,barbcolor='k',flagcolor='r',linewidth=0.5)
4343
# plot colorbar for pressure
44-
cax = plt.axes([0.875, 0.1, 0.05, 0.8]) # setup colorbar axes.
45-
plt.colorbar(cax=cax) # draw colorbar
46-
plt.axes(ax) # make the original axes current again
44+
m.colorbar(pad='12%') # draw colorbar
4745
# draw coastlines
4846
m.drawcoastlines()
4947
# draw parallels
@@ -72,9 +70,7 @@
7270
# plot barbs.
7371
m.barbs(xv,yv,udat,vdat,length=6,barbcolor='k',flagcolor='r',linewidth=0.5)
7472
# plot colorbar for pressure
75-
cax = plt.axes([0.875, 0.1, 0.05, 0.8]) # setup colorbar axes.
76-
plt.colorbar(cax=cax) # draw colorbar
77-
plt.axes(ax) # make the original axes current again
73+
m.colorbar(pad='12%') # draw colorbar
7874
# draw coastlines
7975
m.drawcoastlines()
8076
# draw parallels

examples/contour_demo.py

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
CS1 = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
2424
# fill between contour lines.
2525
CS2 = m.contourf(x,y,hgt,CS1.levels,cmap=plt.cm.jet,extend='both')
26-
# setup colorbar axes instance.
27-
pos = ax.get_position()
28-
l, b, w, h = pos.bounds
29-
cax = plt.axes([l+w+0.075, b, 0.05, h]) # setup colorbar axes
30-
plt.colorbar(CS2, drawedges=True, cax=cax) # draw colorbar
31-
plt.axes(ax) # make the original axes current again
26+
m.colorbar(CS2) # draw colorbar
3227
# draw coastlines and political boundaries.
3328
m.drawcoastlines()
3429
m.drawmapboundary()
@@ -50,11 +45,7 @@
5045
x, y = m(lons, lats)
5146
CS1 = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
5247
CS2 = m.contourf(x,y,hgt,CS1.levels,cmap=plt.cm.jet,extend='both')
53-
pos = ax.get_position()
54-
l, b, w, h = pos.bounds
55-
cax = plt.axes([l+w+0.075, b, 0.05, h]) # setup colorbar axes
56-
plt.colorbar(CS2, drawedges=True, cax=cax) # draw colorbar
57-
plt.axes(ax) # make the original axes current again
48+
m.colorbar(CS2) # draw colorbar
5849
# draw coastlines and political boundaries.
5950
m.drawcoastlines()
6051
m.drawmapboundary()
@@ -76,11 +67,7 @@
7667
x, y = m(lons, lats)
7768
CS1 = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
7869
CS2 = m.contourf(x,y,hgt,CS1.levels,cmap=plt.cm.jet,extend='both')
79-
pos = ax.get_position()
80-
l, b, w, h = pos.bounds
81-
cax = plt.axes([l+w+0.075, b, 0.05, h]) # setup colorbar axes
82-
plt.colorbar(CS2, drawedges=True, cax=cax) # draw colorbar
83-
plt.axes(ax) # make the original axes current again
70+
m.colorbar(CS2) # draw colorbar
8471
# draw coastlines and political boundaries.
8572
m.drawcoastlines()
8673
m.drawmapboundary()
@@ -102,11 +89,7 @@
10289
x, y = m(lons, lats)
10390
CS1 = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
10491
CS2 = m.contourf(x,y,hgt,CS2.levels,cmap=plt.cm.jet,extend='both')
105-
pos = ax.get_position()
106-
l, b, w, h = pos.bounds
107-
cax = plt.axes([l+w+0.075, b, 0.05, h]) # setup colorbar axes
108-
plt.colorbar(CS2, drawedges=True, cax=cax) # draw colorbar
109-
plt.axes(ax) # make the original axes current again
92+
m.colorbar(CS2,pad='12%') # draw colorbar
11093
# draw coastlines and political boundaries.
11194
m.drawcoastlines()
11295
m.drawmapboundary()
@@ -129,11 +112,7 @@
129112
x, y = m(lons, lats)
130113
CS1 = m.contour(x,y,hgt,15,linewidths=0.5,colors='k')
131114
CS2 = m.contourf(x,y,hgt,CS1.levels,cmap=plt.cm.jet,extend='both')
132-
pos = ax.get_position()
133-
l, b, w, h = pos.bounds
134-
cax = plt.axes([l+w+0.075, b, 0.05, h]) # setup colorbar axes
135-
plt.colorbar(CS2, drawedges=True, cax=cax) # draw colorbar
136-
plt.axes(ax) # make the original axes current again
115+
m.colorbar(CS2) # draw colorbar
137116
# draw coastlines and political boundaries.
138117
m.drawcoastlines()
139118
m.fillcontinents()

examples/geos_demo_2.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
# be outside map projection region.
4747
m.drawmeridians(np.arange(10,76,5), labels=[0,0,1,0], color=overlay_color)
4848
m.drawparallels(np.arange(-90,90,5), labels=[1,0,0,0], color=overlay_color)
49-
# add a colobar
50-
#plt.colorbar()
5149
# add timestamp and save
5250
fig = plt.gcf()
5351
fig.text(x=0.275, y=0.025, s=u'Meteosat-9 VIS 0.6 channel - 12:00 UTC 04/06/2007\n \N{COPYRIGHT SIGN} EUMETSAT 2007',

examples/panelplot.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
CS = mnh.contour(xnh,ynh,hgt,15,linewidths=0.5,colors='k')
2929
CS = mnh.contourf(xnh,ynh,hgt,15,cmap=plt.cm.Spectral)
3030
# colorbar on bottom.
31-
pos = ax.get_position()
32-
l, b, w, h = pos.bounds
33-
cax = plt.axes([l, b-0.05, w, 0.025]) # setup colorbar axes
34-
plt.colorbar(cax=cax, orientation='horizontal',ticks=CS.levels[0::4]) # draw colorbar
35-
plt.axes(ax) # make the original axes current again
31+
mnh.colorbar(location='bottom',pad='12%',ticks=CS.levels[0::4])
3632
mnh.drawcoastlines(linewidth=0.5)
3733
delat = 30.
3834
circles = np.arange(0.,90.,delat).tolist()+\
@@ -51,11 +47,7 @@
5147
CS = msh.contour(xsh,ysh,hgt,15,linewidths=0.5,colors='k')
5248
CS = msh.contourf(xsh,ysh,hgt,15,cmap=plt.cm.Spectral)
5349
# colorbar on bottom.
54-
pos = ax.get_position()
55-
l, b, w, h = pos.bounds
56-
cax = plt.axes([l, b-0.05, w, 0.025]) # setup colorbar axes
57-
plt.colorbar(cax=cax,orientation='horizontal',ticks=MultipleLocator(320)) # draw colorbar
58-
plt.axes(ax) # make the original axes current again
50+
msh.colorbar(location='bottom',pad='12%',ticks=MultipleLocator(320))
5951
msh.drawcoastlines(linewidth=0.5)
6052
msh.drawparallels(circles,labels=[1,0,0,0])
6153
msh.drawmeridians(meridians,labels=[1,0,0,1])
@@ -74,11 +66,7 @@
7466
CS = mnh.contour(xnh,ynh,hgt,15,linewidths=0.5,colors='k')
7567
CS = mnh.contourf(xnh,ynh,hgt,15,cmap=plt.cm.RdBu)
7668
# colorbar on right
77-
pos = ax.get_position()
78-
l, b, w, h = pos.bounds
79-
cax = plt.axes([l+w+0.025, b, 0.025, h]) # setup colorbar axes
80-
plt.colorbar(cax=cax, ticks=MultipleLocator(160), format='%4i') # draw colorbar
81-
plt.axes(ax) # make the original axes current again
69+
mnh.colorbar(location='right',ticks=MultipleLocator(160))
8270
mnh.drawcoastlines(linewidth=0.5)
8371
mnh.drawparallels(circles,labels=[1,0,0,0])
8472
mnh.drawmeridians(meridians,labels=[1,0,0,1])
@@ -89,11 +77,7 @@
8977
CS = msh.contour(xsh,ysh,hgt,15,linewidths=0.5,colors='k')
9078
CS = msh.contourf(xsh,ysh,hgt,15,cmap=plt.cm.RdBu)
9179
# colorbar on right.
92-
pos = ax.get_position()
93-
l, b, w, h = pos.bounds
94-
cax = plt.axes([l+w+0.025, b, 0.025, h]) # setup colorbar axes
95-
plt.colorbar(cax=cax, ticks=MultipleLocator(160), format='%4i') # draw colorbar
96-
plt.axes(ax) # make the original axes current again
80+
msh.colorbar(location='right',ticks=MultipleLocator(160))
9781
msh.drawcoastlines(linewidth=0.5)
9882
msh.drawparallels(circles,labels=[1,0,0,0])
9983
msh.drawmeridians(meridians,labels=[1,0,0,1])

examples/plotmap_masked.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
topodat,x,y = m.transform_scalar(topoin,lonsin,latsin,nx,ny,returnxy=True)
3131
# create the figure.
3232
fig=plt.figure(figsize=(8,8))
33-
# add an axes, leaving room for colorbar on the right.
34-
ax = fig.add_axes([0.1,0.1,0.7,0.7])
33+
# add an axes.
34+
ax = fig.add_axes([0.1,0.1,0.8,0.8])
3535
# associate this axes with the Basemap instance.
3636
m.ax = ax
3737
# make topodat a masked array, masking values lower than sea level.
@@ -41,11 +41,7 @@
4141
palette.set_bad('aqua', 1.0)
4242
# plot image over map with imshow.
4343
im = m.imshow(topodatm,palette,norm=colors.normalize(vmin=0.0,vmax=3000.0,clip=False))
44-
# setup colorbar axes instance.
45-
pos = ax.get_position()
46-
l, b, w, h = pos.bounds
47-
cax = plt.axes([l+w+0.075, b, 0.05, h])
48-
plt.colorbar(im,cax=cax) # draw colorbar
44+
m.colorbar(im,pad='12%') # draw colorbar
4945
# plot blue dot on boulder, colorado and label it as such.
5046
xpt,ypt = m(-104.237,40.125)
5147
m.plot([xpt],[ypt],'bo')

examples/plotprecip.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
llcrnrlon=loncorners[0],urcrnrlon=loncorners[2],\
3333
rsphere=6371200.,resolution='l',area_thresh=10000)
3434
# create figure
35-
fig = plt.figure(figsize=(6,8.5))
35+
fig = plt.figure(figsize=(8.5,11))
3636
plt.subplot(211)
3737
ax = plt.gca()
3838
# draw coastlines, state and country boundaries, edge of map.
@@ -53,13 +53,9 @@
5353
# draw filled contours.
5454
clevs = [0,1,2.5,5,7.5,10,15,20,30,40,50,70,100,150,200,250,300,400,500,600,750]
5555
cs = m.contourf(x,y,data,clevs,cmap=cm.s3pcpn)
56-
# new axis for colorbar.
57-
pos = ax.get_position()
58-
l, b, w, h = pos.bounds
59-
cax = plt.axes([l+w+0.025, b, 0.025, h]) # setup colorbar axes
6056
# draw colorbar.
61-
cb = plt.colorbar(cs, cax, format='%g', ticks=clevs, drawedges=False)
62-
plt.axes(ax) # make the original axes current again
57+
cbar = m.colorbar(cs,location='bottom',pad="10%")
58+
cbar.set_label('mm')
6359
# plot title
6460
plt.title(plottitle+'- contourf',fontsize=10)
6561

@@ -79,16 +75,12 @@
7975
# colormap to linear version of the precip colormap.
8076
im2 = copy.copy(im)
8177
im2.set_cmap(cm.s3pcpn_l)
82-
# new axis for colorbar.
83-
pos = ax.get_position()
84-
l, b, w, h = pos.bounds
85-
cax = plt.axes([l+w+0.025, b, 0.025, h]) # setup colorbar axes
86-
# using im2, not im (hack to prevent colors from being
78+
# draw colorbar using im2, not im (hack to prevent colors from being
8779
# too compressed at the low end on the colorbar - results
8880
# from highly nonuniform colormap)
89-
cb = plt.colorbar(im2, cax, format='%d') # draw colorbar
90-
plt.axes(ax) # make the original axes current again
91-
# reset colorbar tick labels (hack to get
81+
cb = m.colorbar(im2,location='bottom',pad="10%")
82+
cb.set_label('mm')
83+
# reset colorbar tick labels.
9284
cb.set_ticks(np.linspace(clevs[0],clevs[-1],len(clevs)))
9385
cb.set_ticklabels(['%g' % clev for clev in clevs])
9486
# plot title

examples/plotsst.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
from mpl_toolkits.basemap import Basemap, date2index, num2date
2-
from netCDF4 import Dataset as NetCDFFile
1+
from mpl_toolkits.basemap import Basemap
2+
from netCDF4 import Dataset, date2index, num2date
33
import numpy as np
44
import matplotlib.pyplot as plt
5-
import sys, datetime
6-
# read in sea-surface temperature and ice data
7-
# can be a local file, a URL for a remote opendap dataset,
8-
if len(sys.argv) == 1:
9-
date = '20071215'
10-
else:
11-
date = sys.argv[1]
12-
# convert datestring to datetime object.
13-
date = datetime.datetime(int(date[0:4]),int(date[4:6]),int(date[6:8]))
14-
print date
5+
import datetime
6+
# create datetime object for desired time
7+
date = datetime.datetime(2007,12,15,0)
158
# open dataset.
169
dataset =\
17-
NetCDFFile('http://nomads.ncdc.noaa.gov/thredds/dodsC/oisst2/totalAmsrAgg')
10+
Dataset('http://nomads.ncdc.noaa.gov/thredds/dodsC/oisst2/totalAmsrAgg')
1811
# find index of desired time.
1912
time = dataset.variables['time']
2013
nt = date2index(date, time, calendar='standard')
21-
print num2date(time[nt],time.units, calendar='standard')
2214
# read sst. Will automatically create a masked array using
2315
# missing_value variable attribute.
2416
sst = dataset.variables['sst'][nt]
@@ -29,18 +21,20 @@
2921
lons = dataset.variables['lon'][:]
3022
# shift lats, lons so values represent edges of grid boxes
3123
# (as pcolor expects).
32-
delon = lons[1]-lons[0]
33-
delat = lats[1]-lats[0]
24+
delon = lons[1]-lons[0]; delat = lats[1]-lats[0]
3425
lons = (lons - 0.5*delon).tolist()
3526
lons.append(lons[-1]+delon)
3627
lons = np.array(lons,np.float64)
3728
lats = (lats - 0.5*delat).tolist()
3829
lats.append(lats[-1]+delat)
3930
lats = np.array(lats,np.float64)
40-
# create Basemap instance for mollweide projection.
31+
# creat figure, axes instances.
32+
fig = plt.figure()
33+
ax = fig.add_axes([0.05,0.05,0.9,0.9])
34+
# create Basemap instance for Robinson projection.
4135
# coastlines not used, so resolution set to None to skip
4236
# continent processing (this speeds things up a bit)
43-
m = Basemap(projection='moll',lon_0=lons.mean(),lat_0=0,resolution=None)
37+
m = Basemap(projection='robin',lon_0=lons.mean(),resolution=None)
4438
# compute map projection coordinates of grid.
4539
x, y = m(*np.meshgrid(lons, lats))
4640
# draw line around map projection limb.
@@ -53,8 +47,8 @@
5347
# draw parallels and meridians, but don't bother labelling them.
5448
m.drawparallels(np.arange(-90.,120.,30.))
5549
m.drawmeridians(np.arange(0.,420.,60.))
56-
# draw horizontal colorbar.
57-
plt.colorbar(im1,orientation='horizontal')
58-
# display the plot with a title.
50+
# add colorbar
51+
cb = m.colorbar(im1,"bottom", size="5%", pad="2%")
52+
# add a title.
5953
plt.title('SST and ICE analysis for %s'%date)
6054
plt.show()

examples/quiver_demo.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
# plot wind vectors over map.
3737
Q = m.quiver(x,y,urot,vrot) #or specify, e.g., width=0.003, scale=400)
3838
qk = plt.quiverkey(Q, 0.95, 1.05, 25, '25 m/s', labelpos='W')
39-
cax = plt.axes([0.875, 0.1, 0.05, 0.7]) # setup colorbar axes.
40-
plt.colorbar(cax=cax) # draw colorbar
41-
plt.axes(ax) # make the original axes current again
39+
m.colorbar(pad='12%') # draw colorbar
4240
m.drawcoastlines()
4341
m.drawcountries()
4442
# draw parallels
@@ -72,9 +70,7 @@
7270
# plot wind vectors over map.
7371
Q = m.quiver(xv,yv,udat,vdat) #or specify, e.g., width=0.003, scale=400)
7472
qk = plt.quiverkey(Q, 0.95, 1.05, 25, '25 m/s', labelpos='W')
75-
cax = plt.axes([0.875, 0.1, 0.05, 0.7]) # setup colorbar axes.
76-
plt.colorbar(cax=cax) # draw colorbar
77-
plt.axes(ax) # make the original axes current again
73+
m.colorbar(pad='12%') # draw colorbar
7874
m.drawcoastlines()
7975
m.drawcountries()
8076
# draw parallels

examples/randompoints.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
# (otherwise they are covered up when continents are filled)
3131
m.scatter(x,y,25,z,cmap=plt.cm.jet,marker='o',edgecolors='none',zorder=10)
3232
# plot colorbar for markers.
33-
plt.colorbar()
33+
m.colorbar()
3434
# create a list of strings containing z values
3535
# or, plot actual numbers as color-coded text strings.
3636
#zn = [ '%2i' % zz for zz in z ]

examples/testgdal.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
# plot DEM image on map.
3434
im = m.imshow(topodat,cmap=cm.GMT_haxby_r)
3535
# draw meridians and parallels.
36-
m.drawparallels(np.arange(20,71,10),labels=[1,0,1,0])
37-
m.drawmeridians(np.arange(-120,-40,10),labels=[0,1,0,1])
36+
m.drawparallels(np.arange(20,71,10),labels=[1,0,0,0])
37+
m.drawmeridians(np.arange(-120,-40,10),labels=[0,0,0,1])
3838
# plot state boundaries from shapefile using ogr.
3939
g = ogr.Open ("st99_d00.shp")
4040
L = g.GetLayer(0) # data is in 1st layer.
@@ -58,13 +58,7 @@
5858
lats = [g.GetY(i) for i in range(g.GetPointCount())]
5959
x, y = m(lons,lats)
6060
m.plot(x,y,'k')
61-
# new axis for colorbar.
62-
ax = plt.gca()
63-
pos = ax.get_position()
64-
l, b, w, h = pos.bounds
65-
cax = plt.axes([l+w+0.05, b, 0.025, h]) # setup colorbar axes
6661
# draw colorbar.
67-
plt.colorbar(im, cax=cax)
68-
plt.axes(ax) # make the original axes current again
62+
m.colorbar(im)
6963
plt.title(gd.GetDescription()+' with state boundaries from '+g.GetName(),y=1.05)
7064
plt.show()

0 commit comments

Comments
 (0)