本文主要介绍GeoPandas结合matplotlib实现地图的基础可视化。GeoPandas是一个Python开源项目,旨在提供丰富而简单的地理空间数据处理接口。GeoPandas扩展了Pandas的数据类型,并使用matplotlib进行绘图。GeoPandas官方仓库地址为:GeoPandas。GeoPandas的官方文档地址为:GeoPandas-doc。关于GeoPandas的使用见
GeoPandas推荐使用Python3.7版本及以上,运行环境最好是linux系统。GeoPandas安装命令如下:
pip install geopandas
如果上述命令安装出问题,则推荐使用conda安装GeoPandas,命令如下:
conda install geopandas
或:
conda install --channel conda-forge geopand
# jupyter notebook环境去除warning |
|
import warnings |
|
warnings.filterwarnings("ignore") |
|
# 查看geopandas版本 |
|
import geopandas as gpd |
|
gpd.__version__ |
'0.10.2' |
1 基础绘图
1.1 绘图接口说明
GeoPandas基于matplotlib库封装高级接口用于制作地图,GeoSeries或GeoDataFrame对象都提供了plot函数以进行对象可视化。与GeoSeries对象相比,GeoDataFrame对象提供的plot函数在参数上更为复杂,也更为常用。
GeoDataFrame对象提供的plot函数的常用输入参数如下:
def GeoDataFrame.plot( |
|
column: str, np.array, pd.Series (default None), # 用于绘图的列名或数据列 |
|
kind: str, # 绘图类型 |
|
cmap: str, # matplotlib的颜色图Colormaps |
|
color: str, np.array, pd.Series (default None), # 指定所有绘图对象的统一颜色 |
|
ax: matplotlib.pyplot.Artist (default None), # 指定matplotlib的绘图轴 |
|
cax: matplotlib.pyplot Artist (default None), # 设置图例的坐标轴 |
|
categorical: bool (default False), # 是否按照类别设置对象颜色 |
|
legend: bool (default False), # 是否显示图例,如果column或color参数未赋值,则此参数无效 |
|
scheme: str (default None), # 用于控制分层设色 |
|
k:int (default 5), # scheme的层次数 |
|
vmin:None or float (default None), # 图例cmap的最小值 |
|
vmax:None or float (default None), # 图例cmap的最大值 |
|
markersize:str or float or sequence (default None), # 绘图点的大小 |
|
figsize: tuple of integers (default None), # 用于控制matplotlib.figure.Figure |
|
legend_kwds: dict (default None), # matplotlib图例参数 |
|
missing_kw: dsdict (default None), # 缺失值区域绘制参数 |
|
aspect:‘auto’, ‘equal’, None or float (default ‘auto’), # 设置绘图比例 |
|
**style_kwds: dict, # 其他参数,如对象边界色edgecolor, 对象填充色facecolor, 边界宽linewidth,透明度alpha |
|
)->ax: matplotlib axes instance |
GeoSeries对象提供的plot函数的常用输入参数如下:
def GeoSeries.plot( |
|
s: Series, # GeoSeries对象 |
|
cmap: str (default None), |
|
color: str, np.array, pd.Series, List (default None), |
|
ax: matplotlib.pyplot.Artist (default None), |
|
figsize: pair of floats (default None), |
|
aspect: ‘auto’, ‘equal’, None or float (default ‘auto’), |
|
**style_kwds: dict |
|
)->ax: matplotlib axes instance |
|
想要更好使用以上参数最好熟悉matplotlib,如有不懂可以查阅matplotlib文档。下面简要介绍GeoPandas中绘图函数的使用。
读取数据集
import geopandas as gpd |
|
# 读取自带世界地图数据集 |
|
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) |
|
# 人口,大洲,国名,国家缩写,ISO国家代码,gdp,地理位置数据 |
|
world.head() |
|
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... |
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| 3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... |
| 4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... |
world.plot() |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b270fe10> |
# 读取自带世界各大城市数据集 |
|
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities')) |
|
# 名字,地理位置数据 |
|
cities.head() |
| name | geometry | |
|---|---|---|
| 0 | Vatican City | POINT (12.45339 41.90328) |
| 1 | San Marino | POINT (12.44177 43.93610) |
| 2 | Vaduz | POINT (9.51667 47.13372) |
| 3 | Luxembourg | POINT (6.13000 49.61166) |
| 4 | Palikir | POINT (158.14997 6.91664) |
分区统计图
GeoPandas可以轻松创建分区统计图(每个形状的颜色基于关联变量值的地图)。只需在plot函数中将参数column设置为要用于指定颜色的列。
# 去除南极地区 |
|
world = world[(world.pop_est>0) & (world.name!="Antarctica")] |
|
# 计算人均gpd |
|
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est |
|
# 绘图 |
|
world.plot(column='gdp_per_cap') |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b241f390> |

图例设置
legend=True即可展示图例。
import matplotlib.pyplot as plt |
|
fig, ax = plt.subplots(1, 1) |
|
# 根据人口绘图数绘制 |
|
world.plot(column='pop_est', ax=ax, legend=True) |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b23585d0> |

但是我们会发现绘图区域和图例区域不对齐,我们通过mpl_toolkits设置图例轴,并通过cax参数直接控制plot函数的图例绘制。
from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
fig, ax = plt.subplots(1, 1) |
|
divider = make_axes_locatable(ax) |
|
# 设置图例,right表示位置,size=5%表示图例宽度,pad表示图例离图片间距 |
|
cax = divider.append_axes("right", size="5%", pad=0.2) |
|
world.plot(column='pop_est', ax=ax, legend=True, cax=cax) |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b21de650> |

此外我们可以通过legend_kwds设置图例信息和方向,就像matplotlib那样。
import matplotlib.pyplot as plt |
|
fig, ax = plt.subplots(1, 1) |
|
world.plot(column='pop_est', |
|
ax=ax, |
|
legend=True, |
|
legend_kwds={'label': "Population by Country and Area", |
|
'orientation': "horizontal"}) |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b20ec110> |

颜色设置
GeoPandas的绘图函数中通过cmap参数设置颜色图,具体cmap参数的设置可以见matplotlib颜色图选择教程。
# 设置颜色图为tab20 |
|
world.plot(column='gdp_per_cap', cmap='tab20') |
<matplotlib.axes._subplots.AxesSubplot at 0x7f17b205a490> |

如果只想绘制边界,可以调用boundary属性进行绘制。
world.boundary.plot() |
<matplotlib.axes._subplots.AxesSubplot at 0x7f15ac1cecd0> |

缺失数据绘制
当绘制项某些地图的数据缺失时,GeoPandas会自动放弃这些区域数据的绘制。如下所示。将Africa地区的gdp_per_cap数据设置为确实,那么将不在地图上绘制Africa地区的图形。
import numpy as np |
|
world.loc[world[world.continent=="Africa"].index, 'gdp_per_cap'] = np.nan |
|
world.plot(column='gdp_per_cap') |
<matplotlib.axes._subplots.AxesSubplot at 0x7f15ac0afe50> |

1万+

被折叠的 条评论
为什么被折叠?



