在Jupyter Notebook中使用geopy和matplotlib绘制地图 [英] Plotting a Map with geopy and matplotlib in Jupyter Notebook

查看:1194
本文介绍了在Jupyter Notebook中使用geopy和matplotlib绘制地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制美国地图并标记全国各地的城市.我得到了要工作的地图.但是我遇到两个问题:第一个是,我收到此错误消息:

I am trying to plot a map of the US and mark the various cities across the country. I got the map to work. But I am having two issues: the first is, I am getting this error message:

AttributeError:"NoneType"对象没有属性经度"

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

第二,我尝试使用plt.figsize属性来放大图形,但是我的地图仍然保持不变.

Secondly, I have tried to enlarge the graph using the plt.figsize attribute however my map still stays the same size.

最后,这并不是真正的问题,但是如果我想用城市名称标记点,该怎么办?

Lastly, this is not really an issue but what if i wanted to label the dots with the city names How can i do so?

这是我的地图代码:

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

推荐答案

  1. 我想您的city_list中存在某些问题,Nominatim无法解决.我在下面添加了一张支票.

  1. I guess there is something in your city_list Nominatim can't resolve. I added a check for that below.

您必须先致电figure(num=1,figsize=(8,9)) ,然后再绘制任何内容(此处为地图).

You have to call figure(num=1,figsize=(8,9)) before you plot anything (here: the map).

您可以使用 plt.annotate ,请参见在下面.

You can use plt.annotate, see below.

希望这会有所帮助.

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

这篇关于在Jupyter Notebook中使用geopy和matplotlib绘制地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆