回答
正如@unutbu所说,托马斯的职位here正是你所追求的。
如果您想Cartopy,相应的代码(在V0.7)要做到这一点可以从http://scitools.org.uk/cartopy/docs/latest/tutorials/using_the_shapereader.html略有调整:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
import itertools
import numpy as np
shapename = 'admin_0_countries'
countries_shp = shpreader.natural_earth(resolution='110m',
category='cultural', name=shapename)
# some nice "earthy" colors
earth_colors = np.array([(199, 233, 192),
(161, 217, 155),
(116, 196, 118),
(65, 171, 93),
(35, 139, 69),
])/255.
earth_colors = itertools.cycle(earth_colors)
ax = plt.axes(projection=ccrs.PlateCarree())
for country in shpreader.Reader(countries_shp).records():
print country.attributes['name_long'], earth_colors.next()
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=earth_colors.next(),
label=country.attributes['name_long'])
plt.show()
由pelson回答启发,我发布我有的解决方案。我会留给你最好的,所以我现在不会接受任何答案。
#! /usr/bin/env python
import sys
import os
from pylab import *
from mpl_toolkits.basemap import Basemap
import matplotlib as mp
from shapelib import ShapeFile
import dbflib
from matplotlib.collections import LineCollection
from matplotlib import cm
def get_shapeData(shp,dbf):
for npoly in range(shp.info()[0]):
shpsegs = []
shpinfo = []
shp_object = shp.read_object(npoly)
verts = shp_object.vertices()
rings = len(verts)
for ring in range(rings):
if ring == 0:
shapedict = dbf.read_record(npoly)
name = shapedict["name_long"]
continent = shapedict["continent"]
lons, lats = zip(*verts[ring])
if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91:
raise ValueError,msg
x, y = m(lons, lats)
shpsegs.append(zip(x,y))
shapedict['RINGNUM'] = ring+1
shapedict['SHAPENUM'] = npoly+1
shpinfo.append(shapedict)
lines = LineCollection(shpsegs,antialiaseds=(1,))
lines.set_facecolors(cm.jet(np.random.rand(1)))
lines.set_edgecolors('k')
lines.set_linewidth(0.3)
ax.add_collection(lines)
if __name__=='__main__':
f=figure(figsize=(10,10))
ax = plt.subplot(111)
m = Basemap(projection='merc',llcrnrlat=30,urcrnrlat=72,\
llcrnrlon=-40,urcrnrlon=50,resolution='c')
m.drawcountries(linewidth=0.1,color='w')
sfile = 'ne_10m_admin_0_countries'
shp = ShapeFile(sfile)
dbf = dbflib.open(sfile)
get_shapeData(shp,dbf)
show()
sys.exit(0)
这是结果
这里是我的榜样如何填写阿尔巴尼亚在正确的颜色(不是很优雅,我知道;))。
#HACK for Albania
shpsegs = []
shpinfo = []
shp_object = shp.read_object(9)
verts = shp_object.vertices()
rings = len(verts)
for ring in range(rings):
if ring == 0:
shapedict = dbf.read_record(9)
name = shapedict["name_long"]
continent = shapedict["continent"]
lons, lats = zip(*verts[ring])
if max(lons) > 721. or min(lons) < -721. or max(lats) > 91. or min(lats) < -91:
raise ValueError,msg
x, y = m(lons, lats)
shpsegs.append(zip(x,y))
shapedict['RINGNUM'] = ring+1
shapedict['SHAPENUM'] = npoly+1
shpinfo.append(shapedict)
lines = LineCollection(shpsegs,antialiaseds=(1,))
if name == 'Albania':
lines.set_facecolors('w')
lines.set_edgecolors('k')
lines.set_linewidth(0.3)
ax.add_collection(lines)
重要的是,在完成所有其他形状后,请执行此操作。也许你可以摆脱这段代码的某些部分,但正如我所说,这对我来说已经足够了。
对于我的应用程序的彩色contries通过名称或大陆,因此,这些行:
name = shapedict["name_long"]
continent = shapedict["continent"]
使用我从这个网站获得的数据:http://www.naturalearthdata.com/
你的阿尔巴尼亚人沉没。没有多少人会注意到:D – theta
是的,亚美尼亚的情况实际上也是如此。之后我必须明确填补这两个国家的工作空间。与naturalearthdata的人询问并不确定,一旦我为我解决这个问题,我没有跟进这个问题 –
@red_tiger我和阿根廷和安哥拉有同样的问题。你能把你的解决方案发布到“阿尔巴尼亚问题”吗? NaturalEarth的人们说什么?谢谢。 –
- 1. 基于国家选择,需要填充国家和本市
- 2. CountrySelect表单字段,预填充国家
- 3. 填写世界+美国国家地图
- 4. 如何在地图轮廓上填充国家颜色
- 5. 如何使用ASP.NET填充世界地图中的国家?
- 6. 地图JavaScript API自动填充 - 一个国家的CITIES
- 7. 根据变量值向地图库填充国家和大洲
- 8. 填充状态DDL使用国家DDL在LINQ
- 9. 如何在jsp中预先填充国家
- 10. 在asp.net中填充国家和城市的javascript下载列表
- 11. 在底部设置填充
- 12. 填充树底部
- 13. 如何在谷歌地图过滤自动填充国家ios sdk?
- 14. 填充在python
- 15. ggplot不会填充美国地图
- 16. Mapbox:在底图上只显示一个国家?
- 17. 填充颜色Python图形(印度国旗)
- 18. 从国家代码在python中获取国家名称?
- 19. STRUTS2和AJAX - 选择国家,填写国家
- 20. 使用Javascript - 填写国家代码下拉国家下拉
- 21. html5 doctype图像填充底部bug
- 22. 将Google自动填充列表中的国家/地区的Google地图限制为“印度,美国和英国”
- 23. 如何填充所有国家的下拉列表?
- 24. 如何限制谷歌自动填充到特定的国家?
- 25. Google地方自动填充API国家/地区限制
- 26. Google地方信息自动填充国家/地区限制
- 27. 当选择国家时,Opencart地区/州不填充
- 28. IOS&Objective C - 如何用一系列国家填充UIPickerView?
- 29. Google Places自动填充API限制国家停止工作
- 30. 限制Google Place自动填充的国家/地区的结果?
或许有用:HTTP://www.geophysique。 be/2011/01/27/matplotlib-basemap-tutorial-07-shapefiles-unleached/ – unutbu
我相信这有助于:http://matplotlib.1069221.n5.nabble.com/How-to-draw-a-specific- country-by-basemap-td15744.html –
感谢您的评论,他们最有帮助。我还发现了一个包含免费国家/地区数据的网站,这正是我正在寻找的内容:[http://www.naturalearthdata.com/](http://www.naturalearthdata.com/) –