Cartopy-在Matplotlib地图上使用“ populated_places” NaturalEarthFeature显示

时间:2019-07-23 14:15:17

标签: matplotlib jupyter-notebook cartopy

我正在制作一张地图,该地图需要参考点以及城市名称和州名称(巴西和哥伦比亚)的标签。从“自然地球”页面中的网络图像可以看出,功能“ populated_places”将满足我的需求。我只需要知道如何使用它即可。

在代码中,我有以下内容:

import cartopy.feature as cf
pop = cf.NaturalEarthFeature(category='cultural',
      name='populated_places', 
      scale='10m', 
      facecolor='none')    

该部分似乎正在工作,因为未给我任何错误或警告。


之后,我尝试使用以下方法将其绘制在地图中:
ax1.scatter('pop.LATITUDE', 'pop.LONGITUDE', projection=ccrs.PlateCarree())

我遇到了如下的attributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-47ca7f66b91e> in <module>
     19 ax1.add_feature(stt, edgecolor='black',facecolor='none')
     20 ax1.add_feature(stt_prv, linewidth=0.5, edgecolor='gray')
---> 21 ax1.scatter('pop.LATITUDE', 'pop.LONGITUDE', projection=ccrs.PlateCarree())
     22 
     23 # ax1.add_feature(cartopy.feature.LAND)

C:\ProgramData\Anaconda3\lib\site-packages\cartopy\mpl\geoaxes.py in scatter(self, *args, **kwargs)
   1435                              '(PlateCarree or RotatedPole).')
   1436 
-> 1437         result = matplotlib.axes.Axes.scatter(self, *args, **kwargs)
   1438         self.autoscale_view()
   1439         return result

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1808                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1809                         RuntimeWarning, stacklevel=2)
-> 1810             return func(ax, *args, **kwargs)
   1811 
   1812         inner.__doc__ = _add_data_doc(inner.__doc__,

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, verts, edgecolors, **kwargs)
   4298                 )
   4299         collection.set_transform(mtransforms.IdentityTransform())
-> 4300         collection.update(kwargs)
   4301 
   4302         if colors is None:

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in update(self, props)
    914 
    915         with cbook._setattr_cm(self, eventson=False):
--> 916             ret = [_update_property(self, k, v) for k, v in props.items()]
    917 
    918         if len(ret):

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in <listcomp>(.0)
    914 
    915         with cbook._setattr_cm(self, eventson=False):
--> 916             ret = [_update_property(self, k, v) for k, v in props.items()]
    917 
    918         if len(ret):

C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\artist.py in _update_property(self, k, v)
    910                 func = getattr(self, 'set_' + k, None)
    911                 if not callable(func):
--> 912                     raise AttributeError('Unknown property %s' % k)
    913                 return func(v)
    914 

AttributeError: Unknown property projection

这是2019年7月24日的修改

这是一个可以正确绘制代码的代码。

%matplotlib inline
import matplotlib.pyplot as plt
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cf
import cartopy.io.shapereader as shpr

# Retriving the information from web
stt = cf.NaturalEarthFeature(category='cultural', 
    name='admin_0_boundary_lines_land',
    scale='10m',facecolor='none')
stt_prv = cf.NaturalEarthFeature(category='cultural', 
    name='admin_1_states_provinces_lines',
    scale='10m',facecolor='none')
fname = shpr.natural_earth(resolution='10m', category='cultural', name='populated_places')
reader = shpr.Reader(fname)

# Image desing
fig = plt.figure(figsize=(15, 6))

# Main Map
ax = fig.add_subplot(1, 5, (1,4), projection=ccrs.PlateCarree())
ax.set_title('Populated places of the world', fontsize=16)
ax.coastlines()
points = list(reader.geometries())
ax.scatter([point.x for point in points],
           [point.y for point in points],
           transform=ccrs.PlateCarree(),
           s=0.1, c='r')
ax.add_feature(stt, linewidth=0.2, edgecolor='black')

# Brazil Map
ax = fig.add_subplot(2, 5, 5, projection=ccrs.PlateCarree())
ax.set_extent([-74, -34, -30, 6.1])
ax.set_title('Populated places of BRA', fontsize=8)
ax.coastlines()
points = list(reader.geometries())
ax.scatter([point.x for point in points],
           [point.y for point in points],
           transform=ccrs.PlateCarree(),
           marker='p',s=10, c='r')
ax.add_feature(stt,     linewidth=0.5, edgecolor='black')
ax.add_feature(stt_prv, linewidth=0.2, edgecolor='black')

# Colombia Map
ax = fig.add_subplot(2, 5, 10, projection=ccrs.PlateCarree())
ax.set_extent([-65, -80, -5, 13])
ax.set_title('Populated places of COL', fontsize=8)
ax.coastlines()
points = list(reader.geometries())
ax.scatter([point.x for point in points],
           [point.y for point in points],
           transform=ccrs.PlateCarree(),
           marker='p',s=10, c='r')
ax.add_feature(stt,     linewidth=0.5, edgecolor='black', zorder=0)
ax.add_feature(stt_prv, linewidth=0.2, edgecolor='black', zorder=5)

plt.show()

Result of previous code

0 个答案:

没有答案