2016-11-08 182 views
0

您好,我正在嘗試繪製一張美國地圖並標記全國各個城市。我得到了地圖工作。但我有兩個問題。第一個是,我收到此錯誤信息:在Jupyter筆記本上繪製一個包含geopy和matplotlib的地圖

AttributeError: 'NoneType' object has no attribute 'longitude' 

其次,我試圖擴大使用plt.figsize屬性但是我的地圖仍然是一樣大小的圖表。

最後,這不是一個真正的問題,但如果我想用城市名稱標註點,該怎麼辦?

這裏是我的地圖代碼:

import matplotlib.pyplot as plt 
from mpl_toolkits.basemap import Basemap 
from geopy.geocoders import Nominatim 
import math 

city_list = list(flight_data["OriginCityName"].unique()) 
cities = city_list 
scale = 1 

map = Basemap(width=10000000,height=6000000,projection='lcc', 
      resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.) 
plt.figure(figsize=(19,20)) 
map.bluemarble() 


# Get the location of each city and plot it 
geolocator = Nominatim() 
for city in cities: 
     loc = geolocator.geocode(city) 
     if not loc: 
      print("Could not locate {}".format(city)) 
      continue 
     x, y = map(loc.longitude, loc.latitude) 
     map.plot(x,y,marker='o',color='Red',markersize=5) 
     plt.annotate(city, xy = (x,y), xytext=(-20,20)) 
plt.show() 

enter image description here

回答

1
  1. 我想有東西在你的city_list Nominatim無法解決。我在下面添加了一張支票。

  2. 您必須致電figure(num=1,figsize=(8,9))之前你繪製任何東西(這裏:地圖)。

  3. 您可以使用plt.annotate,見下文。

希望這會有所幫助。

for city in cities: 
     loc = geolocator.geocode(city) 
     if not loc: 
      print("Could not locate {}".format(city)) 
      continue 
     x, y = map(loc.longitude, loc.latitude) 
     map.plot(x,y,marker='o',color='Red',markersize=int(math.sqrt(count))*scale) 
     plt.annotate(city, xy = (x,y), xytext=(-20,20)) 
+0

嘿figsize作品。但是當我使用for循環,你給我在地圖上的紅點不出現,也plt.annotate不工作,它說標籤沒有定義 –

+0

@DeepakM我覺得我有點不清楚。我只發佈了for循環的開始修改。我在上面的答案中加入了其餘部分。 標籤是點旁邊顯示的城市名稱。上面也加了。 –

+0

嗨,男士真的很感謝所有幫助對不起,再次打擾你,但爲什麼它通過一個錯誤NameError:名稱'計數'沒有定義@ e-dschungel –