gpt4 book ai didi

python - basemap 上的国家标签

转载 作者:太空狗 更新时间:2023-10-29 21:46:02 24 4
gpt4 key购买 nike

我想在 basemap 上绘制轨迹,并将国家/地区标签(名称)显示为叠加层。

这是当前代码及其生成的 map :

import pandas as pd 
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap


path = "path\\to\\data"

animal_data = pd.DataFrame.from_csv(path, header=None)
animal_data.columns = ["date", "time", "gps_lat", "gps_long"]

# data cleaning omitted for clarity

params = {
'projection':'merc',
'lat_0':animal_data.gps_lat.mean(),
'lon_0':animal_data.gps_long.mean(),
'resolution':'h',
'area_thresh':0.1,
'llcrnrlon':animal_data.gps_long.min()-10,
'llcrnrlat':animal_data.gps_lat.min()-10,
'urcrnrlon':animal_data.gps_long.max()+10,
'urcrnrlat':animal_data.gps_lat.max()+10
}
map = Basemap(**params)

map.drawcoastlines()
map.drawcountries()
map.fillcontinents(color = 'coral')
map.drawmapboundary()

x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values)

map.plot(x, y, 'b-', linewidth=1)
plt.show()

map 的结果是: Migration

这是一张候鸟的轨迹图。虽然这是一张非常漂亮的 map (!),但我需要国家/地区名称标签,以便轻松确定这只鸟飞过的国家/地区。

是否有添加国家/地区名称的直接方法?

最佳答案

我的解决方案依赖于一个外部数据文件,该文件将来可能可用也可能不可用。然而,类似的数据可以在其他地方找到,所以这应该不是什么大问题。

首先是打印国名标签的代码:

import pandas as pd 
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

class MyBasemap(Basemap):
def printcountries(self, d=3, max_len=12):
data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt",
sep=";", skiprows=28 )
data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)]
for ix, country in data.iterrows():
plt.text(*self(country.longitude, country.latitude), s=country.BGN_name[:max_len])

所有这一切都是从 here 下载一个国家位置数据库。 ,然后选择当前位于 map 上的国家/地区,并为其添加标签。

完整代码:

import pandas as pd 
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

class MyBasemap(Basemap):
def printcountries(self, d=3, max_len=12):
data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt",
sep=";", skiprows=28 )
data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)]
for ix, country in data.iterrows():
plt.text(*self(country.longitude, country.latitude), s=country.BGN_name[:max_len])


path = "path\\to\\data"

animal_data = pd.DataFrame.from_csv(path, header=None)
animal_data.columns = ["date", "time", "gps_lat", "gps_long"]

params = {
'projection':'merc',
'lat_0':animal_data.gps_lat.mean(),
'lon_0':animal_data.gps_long.mean(),
'resolution':'h',
'area_thresh':0.1,
'llcrnrlon':animal_data.gps_long.min()-10,
'llcrnrlat':animal_data.gps_lat.min()-10,
'urcrnrlon':animal_data.gps_long.max()+10,
'urcrnrlat':animal_data.gps_lat.max()+10
}

plt.figure()
map = MyBasemap(**params)

map.drawcoastlines()
map.fillcontinents(color = 'coral')
map.drawmapboundary()
map.drawcountries()
map.printcountries()

x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values)

map.plot(x, y, 'b-', linewidth=1)
plt.show()

最后,结果:

labeld-map

显然,这并没有像人们希望的那样仔细标记,应该实现一些关于国家大小、名称长度和 map 大小的试探法以使其完美,但这是一个很好的起点。

关于python - basemap 上的国家标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com