# 11.3. Cartopy绘图要素¶

## 11.3.1. 在cartopy中修改地图的边界/直线¶

>>> %matplotlib inline
>>>
>>> import matplotlib.path as mpath
>>> import matplotlib.pyplot as plt
>>>
>>> import cartopy.crs as ccrs
>>>
>>>
>>> fig = plt.figure()
>>> ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.PlateCarree())
>>> ax.coastlines()
>>>
>>> # Construct a star in longitudes and latitudes.
>>> star_path = mpath.Path.unit_regular_star(5, 0.5)
>>> star_path = mpath.Path(star_path.vertices.copy() * 80,
>>>                        star_path.codes.copy())
>>>
>>> # Use the star as the boundary.
>>> ax.set_boundary(star_path, transform=ccrs.PlateCarree())
>>>
>>> plt.show()


## 11.3.2. 箭头¶

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>>
>>> import cartopy.crs as ccrs
>>> import cartopy.feature as cfeature
>>>
>>>
>>> def sample_data(shape=(20, 30)):
>>>     """
>>>     Return (x, y, u, v, crs) of some vector data
>>>     computed mathematically. The returned crs will be a rotated
>>>     pole CRS, meaning that the vectors will be unevenly spaced in
>>>     regular PlateCarree space.
>>>
>>>     """
>>>     crs = ccrs.RotatedPole(pole_longitude=177.5, pole_latitude=37.5)
>>>
>>>     x = np.linspace(311.9, 391.1, shape[1])
>>>     y = np.linspace(-23.6, 24.8, shape[0])
>>>
>>>     x2d, y2d = np.meshgrid(x, y)
>>>     u = 10 * (2 * np.cos(2 * np.deg2rad(x2d) + 3 * np.deg2rad(y2d + 30)) ** 2)
>>>     v = 20 * np.cos(6 * np.deg2rad(x2d))
>>>
>>>     return x, y, u, v, crs
>>>
>>>
>>>
>>> fig = plt.figure()
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.Orthographic(-10, 45))
>>>
>>>
>>> ax.set_global()
>>> ax.gridlines()
>>>
>>> x, y, u, v, vector_crs = sample_data()
>>> ax.quiver(x, y, u, v, transform=vector_crs)
>>>
>>> plt.show()
>>>
>>>


## 11.3.3. 流线图¶

>>> import matplotlib.pyplot as plt
>>>
>>> import cartopy.crs as ccrs
>>> from cartopy.examples.arrows import sample_data
>>>
>>>
>>> fig = plt.figure(figsize=(10, 5))
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
>>> ax.set_extent([-90, 75, 10, 85], crs=ccrs.PlateCarree())
>>> ax.coastlines()
>>>
>>> x, y, u, v, vector_crs = sample_data(shape=(80, 100))
>>> magnitude = (u ** 2 + v ** 2) ** 0.5
>>> ax.streamplot(x, y, u, v, transform=vector_crs,
>>>               linewidth=2, density=2, color=magnitude)
>>> plt.show()


## 11.3.4. 带抖动的向量重拼接¶

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>>
>>> import cartopy.crs as ccrs
>>>
>>>
>>> def sample_data(shape=(20, 30)):
>>>     """
>>>     Return (x, y, u, v, crs) of some vector data
>>>     computed mathematically. The returned CRS will be a North Polar
>>>     Stereographic projection, meaning that the vectors will be unevenly
>>>     spaced in a PlateCarree projection.
>>>
>>>     """
>>>     crs = ccrs.NorthPolarStereo()
>>>     scale = 1e7
>>>     x = np.linspace(-scale, scale, shape[1])
>>>     y = np.linspace(-scale, scale, shape[0])
>>>
>>>     x2d, y2d = np.meshgrid(x, y)
>>>     u = 10 * np.cos(2 * x2d / scale + 3 * y2d / scale)
>>>     v = 20 * np.cos(6 * x2d / scale)
>>>
>>>     return x, y, u, v, crs
>>>
>>>
>>> fig = plt.figure(figsize=(8, 10))
>>>
>>> x, y, u, v, vector_crs = sample_data(shape=(50, 50))
>>> ax1 = fig.add_subplot(2, 1, 1, projection=ccrs.PlateCarree())
>>> ax1.coastlines('50m')
>>> ax1.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())
>>> ax1.quiver(x, y, u, v, transform=vector_crs)
>>>
>>> ax2 = fig.add_subplot(2, 1, 2, projection=ccrs.PlateCarree())
>>> ax2.set_title('The same vector field regridded')
>>> ax2.coastlines('50m')
>>> ax2.set_extent([-45, 55, 20, 80], ccrs.PlateCarree())
>>> ax2.quiver(x, y, u, v, transform=vector_crs, regrid_shape=20)
>>>
>>> plt.show()
>>>


## 11.3.5. 倒钩¶

>>> import matplotlib.pyplot as plt
>>>
>>> import cartopy.crs as ccrs
>>> from cartopy.examples.arrows import sample_data
>>>
>>>
>>> fig = plt.figure(figsize=(10, 5))
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
>>> ax.set_extent([-90, 80, 10, 85], crs=ccrs.PlateCarree())
>>> ax.stock_img()
>>> ax.coastlines()
>>>
>>> x, y, u, v, vector_crs = sample_data(shape=(10, 14))
>>> ax.barbs(x, y, u, v, length=5,
>>>          sizes=dict(emptybarb=0.25, spacing=0.2, height=0.5),
>>>          linewidth=0.95, transform=vector_crs)
>>>
>>> plt.show()
>>>
>>>


## 11.3.6. 填充轮廓¶

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>>
>>> import cartopy.crs as ccrs
>>>
>>>
>>> def sample_data(shape=(73, 145)):
>>>     """Return lons, lats and data of some fake data."""
>>>     nlats, nlons = shape
>>>     lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)
>>>     lons = np.linspace(0, 2 * np.pi, nlons)
>>>     lons, lats = np.meshgrid(lons, lats)
>>>     wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
>>>     mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)
>>>
>>>     data = wave + mean
>>>
>>>     return lons, lats, data
>>>
>>> fig = plt.figure(figsize=(10, 5))
>>> ax = fig.add_subplot(1, 1, 1, projection=ccrs.Mollweide())
>>>
>>> lons, lats, data = sample_data()
>>>
>>> ax.contourf(lons, lats, data,
>>>             transform=ccrs.PlateCarree(),
>>>             cmap='nipy_spectral')
>>> ax.coastlines()
>>> ax.set_global()
>>> plt.show()