Onpick3 悬停最近的数据点而不是列表
Onpick3 Hover nearest data point rather than list
经过几天的努力解决这个问题,我想我会寻求帮助...
我有 3 个列表,lon、lat 和 pop 的长度都相同。用lon[1],lat[1]对应pop[1]。我想做的是,在将它们绘制出来之后,到 'hover over the map' 然后(最终为每个点绘制人口的时间序列)但现在,我只想知道相应的人口值是什么.. .
下面一直在用这个,不知道怎么用
a) 使其到达最近的经纬度点,因为当前它会生成一个可能选项列表
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()
非常感谢您的帮助!
我可能做的很绕,但结果是当我悬停在(放大的地图)上时它只记录了一个点)但是当我很远的时候,这个点包含了几个,在这一点,考虑到地图的密度并且它不会有什么区别,我只是选择了列表中的第一个索引。
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)
经过几天的努力解决这个问题,我想我会寻求帮助...
我有 3 个列表,lon、lat 和 pop 的长度都相同。用lon[1],lat[1]对应pop[1]。我想做的是,在将它们绘制出来之后,到 'hover over the map' 然后(最终为每个点绘制人口的时间序列)但现在,我只想知道相应的人口值是什么.. .
下面一直在用这个,不知道怎么用
a) 使其到达最近的经纬度点,因为当前它会生成一个可能选项列表
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()
非常感谢您的帮助!
我可能做的很绕,但结果是当我悬停在(放大的地图)上时它只记录了一个点)但是当我很远的时候,这个点包含了几个,在这一点,考虑到地图的密度并且它不会有什么区别,我只是选择了列表中的第一个索引。
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)