After several days trying to figure this out, I thought I would ask for help...
I have 3 lists, lon, lat and pop all the same length. With lon[1],lat[1] corresponding to pop[1]. What I want to do, is after having plotted them out, to 'hover over the map' and then (ultimately plot a time series of population for each point) but for now, I just want to know what the corresponding population value is...
i have been using this below, but I don't know how to
a) make it to the nearest lat,lon point, as currently it produces a list of possible options
b) with the figure zoom in then click, because the click zoom in button seems to stop any further points being recorded...
#Import modules
import netCDF4 as nc4
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap
from matplotlib import cm as cm
from matplotlib import mlab as ml
import matplotlib as mpl
from matplotlib.pyplot import figure, show
def extractdata(nc_filename,column_data):
dataset=Dataset(nc_filename) #Reads the data into a column format
output= dataset.variables[column_data][:]
dataset.close()
return(output)
#Start of program
inputfile='reference_pop.nc'
dataset = Dataset(inputfile)
print(dataset.variables.keys())
time=extractdata(inputfile,'time')
lon=extractdata(inputfile,'lon')
lat=extractdata(inputfile,'lat')
pop=extractdata(inputfile,'pop')
index=np.arange(len(lat))
#Reverse the time order (So that 0 is 120,000 years ago aka from past to present
time=time[::-1]
#Reverse population order
pop=pop[::-1] #Population is a 2d matrix, of dimensions pop and len(lon/lat)
def onpick3(event):
ind = event.ind
#print 'onpick3 scatter:', ind, npy.take(lon, ind), npy.take(lat, ind)
print 'ind', ind #Example output: [2513 2673 2843 3022 3023 3024 3025 3203]
print 'npy.take(lon, ind)',npy.take(lon, ind) #Example output [ 21398764. 21459962. 21520490. 21391092. 21454742. 21517902. 21580542. 21577006.]
print 'npy.take(lat, ind)',npy.take(lat, ind) #Example output [ 21398764. 21459962. 21520490. 21391092. 21454742. 21517902. 21580542. 21577006.]
#Will need to reverse back from basemap lat,lon to normal but that is easy
fig = figure()
ax1 = fig.add_subplot(111)
map1 = Basemap(projection='mill',lon_0=0, ax=ax1)
map1.drawmapboundary(fill_color='#9999FF')
##mapping coordinates according to basemap
lon,lat=map1(lon,lat)
ax1.scatter(lon,lat,c=index,cmap=mpl.cm.get_cmap('jet'),picker=1)
fig.canvas.mpl_connect('pick_event', onpick3)
plt.show()
Many thanks for your help!