- Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathrandompoints.py
56 lines (53 loc) · 2.13 KB
/
randompoints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from __future__ import (absolute_import, division, print_function)
importnumpyasnp
importmatplotlib.pyplotasplt
frommatplotlib.colorsimportrgb2hex
frommpl_toolkits.basemapimportBasemap
fromnumpy.randomimportuniform
# Plot a bunch of randomly distributed points on the earth.
# set up stereographic map centered on N. Pole.
m=Basemap(lon_0=-105,boundinglat=20.,round=True,
resolution='l',area_thresh=10000.,projection='npstere')
# number of points to plot.
npts=1000
# generate random points on a sphere,
# so that every small area on the sphere is expected
# to have the same number of points.
# http://mathworld.wolfram.com/SpherePointPicking.html
u=uniform(0.,1.,size=npts)
v=uniform(0.,1.,size=npts)
lons=360.*u
lats= (180./np.pi)*np.arccos(2*v-1) -90.
z=uniform(0,100,size=npts) # this field controls color of dots.
# transform lons and lats to map coordinates.
x,y=m(lons,lats)
# plot them as filled circles on the map.
# first, create a figure.
fig=plt.figure()
# draw colored markers.
# use zorder=10 to make sure markers are drawn last.
# (otherwise they are covered up when continents are filled)
m.scatter(x,y,25,z,cmap=plt.cm.jet,marker='o',edgecolors='none',zorder=10)
# plot colorbar for markers.
m.colorbar(pad='8%')
# create a list of strings containing z values
# or, plot actual numbers as color-coded text strings.
#zn = [ '%2i' % zz for zz in z ]
## plot numbers on map, colored by value.
#for numstr,zval,xpt,ypt in zip(zn,z,x,y):
# # only plot values inside map region.
# if xpt > m.xmin and xpt < m.xmax and ypt > m.ymin and ypt < m.ymax:
# hexcolor = rgb2hex(plt.cm.jet(zval/100.)[:3])
# plt.text(xpt,ypt,numstr,fontsize=9,weight='bold',color=hexcolor)
# draw coasts and fill continents/lakes.
m.drawcoastlines(linewidth=0.5,color='y')
m.drawcountries(color='y')
m.drawstates(color='y')
m.fillcontinents(color='grey',lake_color='black')
# color ocean areas
m.drawmapboundary(fill_color='black')
# draw parallels and meridians.
m.drawparallels(np.arange(-80,81,20),color='y')
m.drawmeridians(np.arange(-180,181,45),labels=[1,1,0,0],color='y')
plt.title('Colored Random Points',y=1.05)
plt.show()