Skip to content

Commit d3dc3f9

Browse files
author
Jeff Whitaker
committed
use hexbin method.
1 parent c91ac1a commit d3dc3f9

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

examples/hexbin_demo.py

Lines changed: 14 additions & 9 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,15 +45,16 @@
4145

4246
# use histogram2d instead of hexbin.
4347
ax = fig.add_subplot(122)
48+
# remove points outside projection limb.
4449
bincount, xedges, yedges = np.histogram2d(x, y, bins=bins)
4550
mask = bincount == 0
4651
# reset zero values to one to avoid divide-by-zero
4752
bincount = np.where(bincount == 0, 1, bincount)
4853
H, xedges, yedges = np.histogram2d(x, y, bins=bins, weights=z)
4954
H = np.ma.masked_where(mask, H/bincount)
50-
# 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)
5156
palette = plt.cm.jet
52-
palette.set_bad('white', 1.0)
57+
palette.set_bad(ax.get_axis_bgcolor(), 1.0)
5358
CS = m.pcolormesh(xedges,yedges,H.T,shading='flat',cmap=palette)
5459
# draw coastlines, lat/lon lines.
5560
m.drawcoastlines()

0 commit comments

Comments
 (0)