目录

上一个主题

11.3. Cartopy绘图要素

下一个主题

11.5. Cartopy地图绘图2

关注公众号


常见问题

  1. Windows下的安装说明
  2. Jupyter免费在线实验环境
  3. 勘误与补充


11.4. Cartopy 地图绘图1

11.4.1. 地图分幅采集

演示cartopy绘制地图块的能力,这些地图块是根据需要从Stamen tile服务器下载的。在内部,这些瓷砖组合成一个单一的图像,并显示在卡通地轴。

>>> %matplotlib inline
>>>
>>> import matplotlib.pyplot as plt
>>> from matplotlib.transforms import offset_copy
>>>
>>> import cartopy.crs as ccrs
>>> import cartopy.io.img_tiles as cimgt
>>>
>>>
>>>
>>> # Create a Stamen terrain background instance.
>>> stamen_terrain = cimgt.Stamen('terrain-background')
>>>
>>> fig = plt.figure()
>>>
>>> # Create a GeoAxes in the tile's projection.
>>> ax = fig.add_subplot(1, 1, 1, projection=stamen_terrain.crs)
>>>
>>> # Limit the extent of the map to a small longitude/latitude range.
>>> ax.set_extent([-22, -15, 63, 65], crs=ccrs.Geodetic())
>>>
>>> # Add the Stamen data at zoom level 8.
>>> ax.add_image(stamen_terrain, 8)
>>>
>>> # Add a marker for the Eyjafjallajökull volcano.
>>> ax.plot(-19.613333, 63.62, marker='o', color='red', markersize=12,
>>>         alpha=0.7, transform=ccrs.Geodetic())
>>>
>>> # Use the cartopy interface to create a matplotlib transform object
>>> # for the Geodetic coordinate system. We will use this along with
>>> # matplotlib's offset_copy function to define a coordinate system which
>>> # translates the text by 25 pixels to the left.
>>> geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax)
>>> text_transform = offset_copy(geodetic_transform, units='dots', x=-25)
>>>
>>> # Add text 25 pixels to the left of the volcano.
>>> ax.text(-19.613333, 63.62, u'Eyjafjallajökull',
>>>         verticalalignment='center', horizontalalignment='right',
>>>         transform=text_transform,
>>>         bbox=dict(facecolor='sandybrown', alpha=0.5, boxstyle='round'))
>>> plt.show()
>>>
>>>
_images/cartopy-map1_2_0.png

11.4.2. 特征创建

此示例手动实例化cartopy.feature.NaturalEarthFeature以访问管理边界(州和省)。 注意,这个例子旨在说明cartopy不一定知道先验知识的构造自然地球特征的能力。但是,在这种情况下,可以使用预定义的cartopy.feature.STATES常量。

>>> import matplotlib.pyplot as plt
>>> import cartopy.crs as ccrs
>>> import cartopy.feature as cfeature
>>> from matplotlib.offsetbox import AnchoredText
>>>
>>>
>>>
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
>>> ax.set_extent([80, 170, -45, 30], crs=ccrs.PlateCarree())
>>>
>>> # Put a background image on for nice sea rendering.
>>> ax.stock_img()
>>>
>>> # Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
>>> states_provinces = cfeature.NaturalEarthFeature(
>>>     category='cultural',
>>>     name='admin_1_states_provinces_lines',
>>>     scale='50m',
>>>     facecolor='none')
>>>
>>> SOURCE = 'Natural Earth'
>>> LICENSE = 'public domain'
>>>
>>> ax.add_feature(cfeature.LAND)
>>> ax.add_feature(cfeature.COASTLINE)
>>> ax.add_feature(states_provinces, edgecolor='gray')
>>>
>>> # Add a text annotation for the license information to the
>>> # the bottom right corner.
>>> text = AnchoredText(r'$\mathcircled{{c}}$ {}; license: {}'
>>>                     ''.format(SOURCE, LICENSE),
>>>                     loc=4, prop={'size': 12}, frameon=True)
>>> ax.add_artist(text)
>>>
>>> plt.show()
>>>
>>>
_images/cartopy-map1_4_0.png

11.4.3. 特征

在cartopy中发现的一些内置自然地球特征的演示。

>>> import cartopy.crs as ccrs
>>> import cartopy.feature as cfeature
>>> import matplotlib.pyplot as plt
>>>
>>>
>>>
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
>>> ax.set_extent([-20, 60, -40, 45], crs=ccrs.PlateCarree())
>>>
>>> ax.add_feature(cfeature.LAND)
>>> ax.add_feature(cfeature.OCEAN)
>>> ax.add_feature(cfeature.COASTLINE)
>>> ax.add_feature(cfeature.BORDERS, linestyle=':')
>>> ax.add_feature(cfeature.LAKES, alpha=0.5)
>>> ax.add_feature(cfeature.RIVERS)
>>>
>>> plt.show()
>>>
/usr/lib/python3/dist-packages/cartopy/io/__init__.py:260: DownloadWarning: Downloading: http://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_boundary_lines_land.zip
  warnings.warn('Downloading: {}'.format(url), DownloadWarning)
/usr/lib/python3/dist-packages/cartopy/io/__init__.py:260: DownloadWarning: Downloading: http://naciscdn.org/naturalearth/110m/physical/ne_110m_lakes.zip
  warnings.warn('Downloading: {}'.format(url), DownloadWarning)
/usr/lib/python3/dist-packages/cartopy/io/__init__.py:260: DownloadWarning: Downloading: http://naciscdn.org/naturalearth/110m/physical/ne_110m_rivers_lake_centerlines.zip
  warnings.warn('Downloading: {}'.format(url), DownloadWarning)
_images/cartopy-map1_6_1.png

11.4.4. 全球地图

一个简单地图的例子,比较两个地点之间的大地测量线和板块卡里线。

>>> import matplotlib.pyplot as plt
>>>
>>> import cartopy.crs as ccrs
>>>
>>>
>>>
>>> fig = plt.figure(figsize=(10, 5))
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.Robinson())
>>>
>>> # make the map global rather than have it zoom in to
>>> # the extents of any plotted data
>>> ax.set_global()
>>>
>>> ax.stock_img()
>>> ax.coastlines()
>>>
>>> ax.plot(-0.08, 51.53, 'o', transform=ccrs.PlateCarree())
>>> ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.PlateCarree())
>>> ax.plot([-0.08, 132], [51.53, 43.17], transform=ccrs.Geodetic())
>>>
>>> plt.show()
_images/cartopy-map1_8_0.png

11.4.5. 夜色特征

绘制给定日期时间没有阳光的多边形。

>>> import datetime
>>> import matplotlib.pyplot as plt
>>> import cartopy.crs as ccrs
>>> from cartopy.feature.nightshade import Nightshade
>>>
>>>
>>> fig = plt.figure(figsize=(10, 5))
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
>>>
>>> date = datetime.datetime(1999, 12, 31, 12)
>>>
>>> ax.set_title('Night time shading for {}'.format(date))
>>> ax.stock_img()
>>> ax.add_feature(Nightshade(date, alpha=0.2))
>>> plt.show()
_images/cartopy-map1_10_0.png

11.4.6. 旋转极盒

在旋转极坐标系中定义长方体时,长方体扭曲方式的演示。 尝试将box_top更改为44、46和75,以查看将极点包含在多边形中所产生的效果。

>>> import matplotlib.pyplot as plt
>>>
>>> import cartopy.crs as ccrs
>>>
>>>
>>>
>>> rotated_pole = ccrs.RotatedPole(pole_latitude=45, pole_longitude=180)
>>>
>>> box_top = 45
>>> x, y = [-44, -44, 45, 45, -44], [-45, box_top, box_top, -45, -45]
>>>
>>> fig = plt.figure()
>>>
>>> ax = fig.add_subplot(2, 1, 1, projection=rotated_pole)
>>> ax.stock_img()
>>> ax.coastlines()
>>> ax.plot(x, y, marker='o', transform=rotated_pole)
>>> ax.fill(x, y, color='coral', transform=rotated_pole, alpha=0.4)
>>> ax.gridlines()
>>>
>>> ax = fig.add_subplot(2, 1, 2, projection=ccrs.PlateCarree())
>>> ax.stock_img()
>>> ax.coastlines()
>>> ax.plot(x, y, marker='o', transform=rotated_pole)
>>> ax.fill(x, y, transform=rotated_pole, color='coral', alpha=0.4)
>>> ax.gridlines()
>>>
>>> plt.show()
>>>
>>>
_images/cartopy-map1_12_0.png