使用 Python 在 Matplotlib Basemap 中将自然地球形状绘制为多边形
Using Python to plot Natural Earth shapes as polygons in Matplotlib Basemap
我即将获得我想要的地图。 Matplotlib 的底图很棒,但是当我放大时海岸线太粗糙了。我可以阅读 Natural Earth shapefile 并绘制它们,这要好得多......但是当我尝试填充多边形时,我认为它正在处理所有的点属于单个多边形。如何遍历多边形并正确显示地图?
提前致谢!
代码如下:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
%matplotlib inline
landColor, coastColor, oceanColor, popColor, countyColor = '#eedd99','#93ccfa','#93ccfa','#ffee99','#aa9955'
fig = plt.figure()
ax = fig.add_subplot(111)
s = 1900000
m = Basemap(projection='ortho',lon_0=-86.5,lat_0=30.3,resolution='l',llcrnrx=-s,llcrnry=-s,urcrnrx=s,urcrnry=s)
m.drawmapboundary(fill_color=oceanColor) # fill in the ocean
# generic function for reading polygons from file and plotting them on the map. This works with Natural Earth shapes.
def drawShapesFromFile(filename,facecolor,edgecolor,m):
m.readshapefile(filename, 'temp', drawbounds = False)
patches = []
for info, shape in zip(m.temp_info, m.temp): patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor=facecolor, edgecolor=edgecolor, linewidths=1))
# read the higher resolution Natural Earth coastline (land polygons) shapefile and display it as a series of polygons
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_coastline',landColor,coastColor,m)
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_urban_areas',popColor,'none',m)
m.drawcounties(color=countyColor)
plt.gcf().set_size_inches(10,10)
根据要求,这是更新后的代码和生成的地图。我所要做的就是将 ne_10m_coastline
更改为 ne_10m_land
,如下所示:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
%matplotlib inline
landColor, coastColor, oceanColor, popColor, countyColor = '#eedd99','#93ccfa','#93ccfa','#ffee99','#aa9955'
fig = plt.figure()
ax = fig.add_subplot(111)
s = 1900000
m = Basemap(projection='ortho',lon_0=-86.5,lat_0=30.3,resolution='l',llcrnrx=-s,llcrnry=-s,urcrnrx=s,urcrnry=s)
m.drawmapboundary(fill_color=oceanColor) # fill in the ocean
# generic function for reading polygons from file and plotting them on the map. This works with Natural Earth shapes.
def drawShapesFromFile(filename,facecolor,edgecolor,m):
m.readshapefile(filename, 'temp', drawbounds = False)
patches = []
for info, shape in zip(m.temp_info, m.temp): patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor=facecolor, edgecolor=edgecolor, linewidths=1))
# read the higher resolution Natural Earth coastline (land polygons) shapefile and display it as a series of polygons
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_land',landColor,coastColor,m)
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_urban_areas',popColor,'none',m)
m.drawcounties(color=countyColor)
plt.gcf().set_size_inches(10,10)
我即将获得我想要的地图。 Matplotlib 的底图很棒,但是当我放大时海岸线太粗糙了。我可以阅读 Natural Earth shapefile 并绘制它们,这要好得多......但是当我尝试填充多边形时,我认为它正在处理所有的点属于单个多边形。如何遍历多边形并正确显示地图?
提前致谢!
代码如下:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
%matplotlib inline
landColor, coastColor, oceanColor, popColor, countyColor = '#eedd99','#93ccfa','#93ccfa','#ffee99','#aa9955'
fig = plt.figure()
ax = fig.add_subplot(111)
s = 1900000
m = Basemap(projection='ortho',lon_0=-86.5,lat_0=30.3,resolution='l',llcrnrx=-s,llcrnry=-s,urcrnrx=s,urcrnry=s)
m.drawmapboundary(fill_color=oceanColor) # fill in the ocean
# generic function for reading polygons from file and plotting them on the map. This works with Natural Earth shapes.
def drawShapesFromFile(filename,facecolor,edgecolor,m):
m.readshapefile(filename, 'temp', drawbounds = False)
patches = []
for info, shape in zip(m.temp_info, m.temp): patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor=facecolor, edgecolor=edgecolor, linewidths=1))
# read the higher resolution Natural Earth coastline (land polygons) shapefile and display it as a series of polygons
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_coastline',landColor,coastColor,m)
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_urban_areas',popColor,'none',m)
m.drawcounties(color=countyColor)
plt.gcf().set_size_inches(10,10)
根据要求,这是更新后的代码和生成的地图。我所要做的就是将 ne_10m_coastline
更改为 ne_10m_land
,如下所示:
import numpy as np
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
%matplotlib inline
landColor, coastColor, oceanColor, popColor, countyColor = '#eedd99','#93ccfa','#93ccfa','#ffee99','#aa9955'
fig = plt.figure()
ax = fig.add_subplot(111)
s = 1900000
m = Basemap(projection='ortho',lon_0=-86.5,lat_0=30.3,resolution='l',llcrnrx=-s,llcrnry=-s,urcrnrx=s,urcrnry=s)
m.drawmapboundary(fill_color=oceanColor) # fill in the ocean
# generic function for reading polygons from file and plotting them on the map. This works with Natural Earth shapes.
def drawShapesFromFile(filename,facecolor,edgecolor,m):
m.readshapefile(filename, 'temp', drawbounds = False)
patches = []
for info, shape in zip(m.temp_info, m.temp): patches.append( Polygon(np.array(shape), True) )
ax.add_collection(PatchCollection(patches, facecolor=facecolor, edgecolor=edgecolor, linewidths=1))
# read the higher resolution Natural Earth coastline (land polygons) shapefile and display it as a series of polygons
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_land',landColor,coastColor,m)
drawShapesFromFile('\Conda\notebooks\shapes\ne_10m_urban_areas',popColor,'none',m)
m.drawcounties(color=countyColor)
plt.gcf().set_size_inches(10,10)