from mpl_toolkits.basemap import Basemap import numpy as np import matplotlib.pyplot as plt import MySQLdb; import math # santa cruz #c_lon = -122.06 #c_lat = 37.0 #l_lon = -123 #l_lat = 36 #r_lon = -121 #r_lat = 38 c_lon = -122.06 c_lat = 37.0 l_lon = -180 l_lat = -90 r_lon = 180 r_lat = 90 con = None try: con = MySQLdb.connect(host='localhost', user='greg', db='wiki') except MySQLdb.Error, e: print "ERROR" + str(e) cur = con.cursor() cur.execute("select gc_lat, gc_lon from coord_enwiki where gc_lat > %s and gc_lat < %s and gc_lon > %s and gc_lon < %s;" % (l_lat, r_lat, l_lon, r_lon)); rows = cur.fetchall() #rows = []; lats = []; longs = []; for row in rows: #print row; lats.append(row[0]) longs.append(row[1]) con.close() print "THIS MANY ARTICLES" print len(rows) fig = plt.figure(figsize=(100/3,80/3)) # llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon # are the lat/lon values of the lower left and upper right corners # of the map. # resolution = 'i' means use intermediate resolution coastlines. # lon_0, lat_0 are the central longitude and latitude of the projection. m = Basemap(llcrnrlon=l_lon,llcrnrlat=l_lat,urcrnrlon=r_lon,urcrnrlat=r_lat, resolution='i',projection='mill',lon_0=c_lon,lat_0=c_lat) #m.drawcoastlines() m.fillcontinents(color='coral',lake_color='aqua', zorder = 0) x,y = m(longs, lats); heatmap, xedges, yedges = np.histogram2d(y, x, bins=(1024*3, 768*3)) extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] for index,value in np.ndenumerate( heatmap ): if(value > 0): value += 3 heatmap[index] = math.log(value) #m.scatter(x,y,1,marker=',',color='k', linewidths= .8, alpha = .6) # draw parallels and meridians. #m.drawparallels(np.arange(-40,61.,2.)) #m.drawmeridians(np.arange(-20.,21.,2.)) m.drawmapboundary(fill_color='aqua') m.imshow(heatmap, cmap = plt.cm.spectral, extent = extent) plt.title("Geocoded En-Wikipedia Articles") #plt.show() plt.draw() plt.savefig("out.png");