0

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!

Luke123
  • 1
  • 3
  • def onpick3(event): ind=event.ind[0] Seems to give the right index. But this is a click not a hover, – Luke123 Nov 08 '17 at 15:48
  • If this is about getting *hover* to work, you may look at [this answer](https://stackoverflow.com/a/47166787/4124317). For nearest neighbor finding, you could look at [this answer](https://stackoverflow.com/a/13306887/4124317). – ImportanceOfBeingErnest Nov 08 '17 at 16:03

1 Answers1

0

I probably did it a very convoluted way, but it turned out that when I was hovering over (a zoomed in map) it only recorded one point) but when I was so far out, the point contained several, at this point,given the density of the map and that it will make no difference, I just selected the first index in the list.

def hover(event):

    #Returns a dictionary
    cont, ind = sc.contains(event)

    #Turn dictionary into a list
    myList = []  
    for k,v in ind.items():
        myList.append(v[0])

    #Take first element as it really doesn't matter, as the indexes are so close together 
    ind=myList[0]
fig1.canvas.mpl_connect("motion_notify_event", hover)
Luke123
  • 1
  • 3