备注

互动在线版: Binder badge

使用GeoPlot和GeoPandas进行打印#

Geoplot 是一个Python库,提供了一系列易于使用的地理空间可视化。它建在较低的一层之上 CartoPy ,在本教程的单独一节中介绍,旨在使用GeoPandas输入。

此示例简要介绍了 geoplot API. For more details on the library refer to its documentation

首先,我们将使用GeoPandas加载数据。

[1]:
import geopandas
import geoplot

world = geopandas.read_file(
    geopandas.datasets.get_path('naturalearth_lowres')
)
boroughs = geopandas.read_file(
    geoplot.datasets.get_path('nyc_boroughs')
)
collisions = geopandas.read_file(
    geoplot.datasets.get_path('nyc_injurious_collisions')
)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 2>()
      1 import geopandas
----> 2 import geoplot
      4 world = geopandas.read_file(
      5     geopandas.datasets.get_path('naturalearth_lowres')
      6 )
      7 boroughs = geopandas.read_file(
      8     geoplot.datasets.get_path('nyc_boroughs')
      9 )

ModuleNotFoundError: No module named 'geoplot'

使用GeoPlot进行打印#

我们首先使用GeoPlot复制基本的GeoPandas世界图。

[2]:
geoplot.polyplot(world, figsize=(8, 4))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 geoplot.polyplot(world, figsize=(8, 4))

NameError: name 'geoplot' is not defined

GeoPlot可以将数据重新投影到CartoPy提供的任何地图投影中(请参阅列表 here )。

[3]:
# use the Orthographic map projection (e.g. a world globe)
ax = geoplot.polyplot(
    world, projection=geoplot.crs.Orthographic(), figsize=(8, 4)
)
ax.outline_patch.set_visible(True)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [3], in <cell line: 2>()
      1 # use the Orthographic map projection (e.g. a world globe)
----> 2 ax = geoplot.polyplot(
      3     world, projection=geoplot.crs.Orthographic(), figsize=(8, 4)
      4 )
      5 ax.outline_patch.set_visible(True)

NameError: name 'geoplot' is not defined

polyplot is trivial and can only plot the geometries you pass to it. If you want to use color as a visual variable, specify a choropleth. Here we sort GDP per person by country into five buckets by color, using “quantiles” binning from the Mapclassify 类库。

[4]:
import mapclassify
gpd_per_person = world['gdp_md_est'] / world['pop_est']
scheme = mapclassify.Quantiles(gpd_per_person, k=5)

# Note: this code sample requires geoplot>=0.4.0.
geoplot.choropleth(
    world, hue=gpd_per_person, scheme=scheme,
    cmap='Greens', figsize=(8, 4)
)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 import mapclassify
      2 gpd_per_person = world['gdp_md_est'] / world['pop_est']
      3 scheme = mapclassify.Quantiles(gpd_per_person, k=5)

ModuleNotFoundError: No module named 'mapclassify'

如果要将大小用作可视变量,请使用 cartogram 。以下是对非洲国家的人口估计。

[5]:
africa = world.query('continent == "Africa"')
ax = geoplot.cartogram(
    africa, scale='pop_est', limits=(0.2, 1),
    edgecolor='None', figsize=(7, 8)
)
geoplot.polyplot(africa, edgecolor='gray', ax=ax)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 africa = world.query('continent == "Africa"')
      2 ax = geoplot.cartogram(
      3     africa, scale='pop_est', limits=(0.2, 1),
      4     edgecolor='None', figsize=(7, 8)
      5 )
      6 geoplot.polyplot(africa, edgecolor='gray', ax=ax)

NameError: name 'world' is not defined

如果我们拥有空间点形状的数据,我们可以使用以下命令在其上生成三维热图 kdeplot

[6]:
ax = geoplot.kdeplot(
    collisions.head(1000), clip=boroughs.geometry,
    shade=True, cmap='Reds',
    projection=geoplot.crs.AlbersEqualArea())
geoplot.polyplot(boroughs, ax=ax, zorder=1)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 ax = geoplot.kdeplot(
      2     collisions.head(1000), clip=boroughs.geometry,
      3     shade=True, cmap='Reds',
      4     projection=geoplot.crs.AlbersEqualArea())
      5 geoplot.polyplot(boroughs, ax=ax, zorder=1)

NameError: name 'geoplot' is not defined

这些只是您可以使用GeoPlot制作的部分地块。还有许多其他可能性没有在这篇简短的介绍中涵盖。有关更多示例,请参阅 Gallery 在GeoPlot文档中。