用geopy和matplotlib在Jupyter Noteb中绘制地图

2024-05-21 19:07:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在画一张美国地图,标出全国各地的城市。我把地图拿来工作了。但我有两个问题:第一,我收到了这个错误消息:

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


Tags: fromimportcitymapplot地图pltloc