Logo
Section 1 / 6

GeoPandas入门:地图绘制

探索 GeoPandas 的可视化基本功能。

导入

GeoPandas入门:地图绘制

探索 GeoPandas 的可视化基本功能。

...
世界国家在正射投影下的地理分布(左图使用 geoplot,右图叠加 rain anomaly 栅格数据并用 contextily 提供背景)
1import geopandas as gpd 2import geodatasets 3 4# 读取数据集 5countries = gpd.read_file(geodatasets.get_path('naturalearth.land')) 6 7# 绘制到自动创建的画布上 8ax = countries.plot(facecolor='green', edgecolor='black') 9ax.set_ylabel('纬度') 10ax.set_xlabel('经度') 11ax.set_facecolor('lightblue') 12ax.figure.savefig('output/countries1.png', dpi=300)
1import numpy as np 2import geopandas as gpd 3import matplotlib.pyplot as plt 4import matplotlib.colors as mcolors 5 6# 读取数据集 7countries = gpd.read_file('countries.geojson') 8 9# 创建人均GDP列 10countries['gdp_per_capta'] = countries['gdp_md_est'] * 1e3 / countries['pop_est'] 11 12# 绘制到已存在的画布上 13fig, axs = plt.subplots(2, 1, figsize=(6, 11)) 14countries.plot(ax=axs[0], column='gdp_per_capta', norm=mcolors.LogNorm()) 15countries.plot(ax=axs[1], kind='scatter', x='pop_est', y='gdp_md_est', color=countries['gdp_per_capta'], norm=mcolors.LogNorm()) 16 17# 添加颜色条 18cbar = fig.colorbar(axs[1].get_children()[0], ax=axs[1], norm=mcolors.LogNorm()) 19cbar.set_label('人均GDP') 20 21# 图形样式设置 22axs[1].set_xscale('log') 23axs[1].set_yscale('log') 24axs[1].set_ylabel('国内生产总值 \n (百万美元)') 25axs[1].set_xlabel('人口数量(人)') 26axs[0].set_ylabel('纬度') 27axs[0].set_xlabel('经度') 28fig.subplots_adjust(hspace=0.01) 29fig.figure.savefig('output/countries2.png', dpi=300)

附录

等值区域图