经过几天试图解决这个问题,我想我会寻求帮助......
我有3个列表,lon,lat和pop的长度相同。使用lon [1],lat [1]对应pop [1]。我想要做的是,在将它们绘制出来之后,将它们悬停在地图上'然后(最终绘制每个点的人口时间序列)但是现在,我只想知道相应的人口价值是多少......
我一直在使用下面这个,但我不知道如何
a)使它到最近的纬度,lon点,因为它目前产生一个可能的选项列表
b)随着图放大然后点击,因为点击放大按钮似乎停止记录任何其他点...
#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()
非常感谢你的帮助!
答案 0 :(得分:0)
我可能是一个非常复杂的方式,但事实证明,当我徘徊(放大地图)时它只记录了一个点)但是当我离得太远时,这个点包含几个,此时,鉴于地图的密度并且没有区别,我只选择了列表中的第一个索引。
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)