Skip to content

Commit 8e3450d

Browse files
author
Jeff Whitaker
committed
Merge pull request #18 from jswhit/master
add hexbin method, restore python 2.4/2.5 compatibility
2 parents 3b074b7 + d3dc3f9 commit 8e3450d

File tree

6 files changed

+278
-221
lines changed

6 files changed

+278
-221
lines changed

Changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
version 1.0.3 (not yet released)
2+
* added hexbin method.
23
* drawmapboundary now uses axes bgcolor as default fill_color. If
34
no color fill wanted, set fill_color='none' (a string).
45
* clip coastlines for nplaea,npaeqd,splaea,spaeqd in stereographic

examples/hexbin_demo.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
# create north polar stereographic basemap
88
m = Basemap(lon_0=270, boundinglat=20, projection='npstere',round=True)
9+
#m = Basemap(lon_0=-105,lat_0=40,projection='ortho')
910

10-
# number of points to plot.
11+
# number of points, bins to plot.
1112
npts = 10000
1213
bins = 40
1314
# generate random points on a sphere,
@@ -16,13 +17,16 @@
1617
# http://mathworld.wolfram.com/SpherePointPicking.html
1718
u = uniform(0.,1.,size=npts)
1819
v = uniform(0.,1.,size=npts)
19-
lons1 = 360.*u
20-
lats1 = (180./np.pi)*np.arccos(2*v-1) - 90.
20+
lons = 360.*u
21+
lats = (180./np.pi)*np.arccos(2*v-1) - 90.
2122
# toss points outside of map region.
22-
lats = np.compress(lats1 > 20, lats1)
23-
lons = np.compress(lats1 > 20, lons1)
23+
lats = np.compress(lats > 20, lats)
24+
lons = np.compress(lats > 20, lons)
2425
# convert to map projection coordinates.
25-
x, y = m(lons, lats)
26+
x1, y1 = m(lons, lats)
27+
# remove points outside projection limb.
28+
x = np.compress(np.logical_or(x1 < 1.e20,y1 < 1.e20), x1)
29+
y = np.compress(np.logical_or(x1 < 1.e20,y1 < 1.e20), y1)
2630
# function to plot at those points.
2731
xscaled = 4.*(x-0.5*(m.xmax-m.xmin))/m.xmax
2832
yscaled = 4.*(y-0.5*(m.ymax-m.ymin))/m.ymax
@@ -31,7 +35,7 @@
3135
# make plot using hexbin
3236
fig = plt.figure(figsize=(12,5))
3337
ax = fig.add_subplot(121)
34-
CS = plt.hexbin(x,y,C=z,gridsize=bins,cmap=plt.cm.jet)
38+
CS = m.hexbin(x,y,C=z,gridsize=bins,cmap=plt.cm.jet)
3539
# draw coastlines, lat/lon lines.
3640
m.drawcoastlines()
3741
m.drawparallels(np.arange(0,81,20))
@@ -41,16 +45,16 @@
4145

4246
# use histogram2d instead of hexbin.
4347
ax = fig.add_subplot(122)
44-
#m = Basemap(lon_0=270, boundinglat=20, projection='npstere',round=True)
48+
# remove points outside projection limb.
4549
bincount, xedges, yedges = np.histogram2d(x, y, bins=bins)
4650
mask = bincount == 0
4751
# reset zero values to one to avoid divide-by-zero
4852
bincount = np.where(bincount == 0, 1, bincount)
4953
H, xedges, yedges = np.histogram2d(x, y, bins=bins, weights=z)
5054
H = np.ma.masked_where(mask, H/bincount)
51-
# set color of masked values to white (hexbin does this by default)
55+
# set color of masked values to axes background (hexbin does this by default)
5256
palette = plt.cm.jet
53-
palette.set_bad('white', 1.0)
57+
palette.set_bad(ax.get_axis_bgcolor(), 1.0)
5458
CS = m.pcolormesh(xedges,yedges,H.T,shading='flat',cmap=palette)
5559
# draw coastlines, lat/lon lines.
5660
m.drawcoastlines()

0 commit comments

Comments
 (0)