2017-11-08 5 views
0

보다는 지적, 나는 내가 위도 3 개에는, 경도를 가지고 있으며 모두 동일한 길이를 나타 나는 ... 도움을Onpick3 호버 가까운 데이터는이를 알아 내려고 몇 일 후에 목록

물어 거라 생각 했어요. lon [1], lat [1]은 pop [1]에 해당합니다. 제가하고 싶은 것은, '지도 위로 마우스를 가져 가면'(그리고 각 점에 대한 인구의 시간적인 연속을 그립니다.)하지만 지금은 해당 인구의 가치가 무엇인지 알고 싶습니다. ..

난 다음이 사용하고있다,하지만 난

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() 

많은 도움을 주셔서 감사합니다.

+0

def onpick3 (이벤트) : ind = event.ind [0] 올바른 색인을 제공합니다. 하지만이 것은 마우스 오버가 아닌 클릭입니다. – Luke123

+0

* 호버가 작동하도록하려면 [이 답변] (https://stackoverflow.com/a/47166787/4124317)을 참조하십시오. 가장 가까운 이웃을 찾기 위해 [이 답변] (https://stackoverflow.com/a/13306887/4124317)을 볼 수 있습니다. – ImportanceOfBeingErnest

답변

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) 
관련 문제