前言
之前使用pandas和cartopy绘制过
https://www.heywhale.com/mw/project/65b0e3448886afac92b3cb5a
来表现全国某个要素在各个省份的分布
现在我们使用geoplot库重新绘制一遍看看效果
!pip install geoplot -i https://pypi.mirrors.ustc.edu.cn/simple/
数据读取与处理
## 读取随机生成的数据
import pandas as pd
import geopandas as gpd
import numpy as np
# 读取shp文件
shp = gpd.read_file('/home/mw/input/china1656/china_map/china_map/China_Province_2022.shp')
random_nums = 1000*np.random.rand(len(shp))
# 将随机数列添加为新的列
shp['Random'] = random_nums
shp.head()
省 | 省级码 | 省类型 | ENG_NAME | VAR_NAME | FIRST_GID | FIRST_TYPE | year | geometry | Random | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 北京市 | 110000 | 直辖市 | Beijing | Běi Jīng | 110000 | Municipality | 2022 | POLYGON ((117.38335 40.22647, 117.38557 40.224... | 841.927184 |
1 | 天津市 | 120000 | 直辖市 | Tianjin | Tiān Jīn | 120000 | Municipality | 2022 | POLYGON ((117.56937 40.19153, 117.56744 40.189... | 807.565088 |
2 | 河北省 | 130000 | 省 | Hebei | Hé Běi | 130000 | Province | 2022 | MULTIPOLYGON (((118.26945 38.98097, 118.26871 ... | 987.744808 |
3 | 山西省 | 140000 | 省 | Shanxi | Shān Xī | 140000 | Province | 2022 | POLYGON ((114.13714 40.73445, 114.13860 40.732... | 202.062701 |
4 | 内蒙古自治区 | 150000 | 自治区 | Neimenggu | Nèi Měng Gǔ | 150000 | Autonomous Region | 2022 | POLYGON ((121.49813 53.32607, 121.50116 53.321... | 804.195501 |
Choropleth Map (分级统计图)
shp.loc[shp['省'] == '广东省', 'Random'] = 10
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
# 设置画布大小
plt.figure(figsize=(16, 10),dpi=300) # 这里设置宽度为10英寸,高度为8英寸,你可以根据需要调整
ax = gplt.choropleth(
shp, hue='Random', projection=gcrs.AlbersEqualArea(),
edgecolor='black', linewidth=1,
cmap='Blues', legend=True,
scheme='FisherJenks',
legend_labels=[
'0-10', '10-100', '100-300',
'300-500', '>500'
]
)
gplt.polyplot(shp, facecolor='lightgray', edgecolor='None', ax=ax)
plt.title("全国压岁钱分布图")
# 显示图形
plt.show()
<Figure size 4800x3000 with 0 Axes>
高清版
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
# 创建一个子图
fig, ax = plt.subplots(1, 1, figsize=(16, 10), dpi=300, subplot_kw={'projection': gcrs.PlateCarree()})
# 绘制填充颜色的地图
gplt.choropleth(
shp, hue='Random', # 'Random'是你的GeoDataFrame中的列名
edgecolor='black', linewidth=1,
cmap='Blues', legend=True,
scheme='FisherJenks',
legend_labels=['0-10', '10-100', '100-300', '300-500', '>500'],
ax=ax
)
# 添加灰色底色的多边形轮廓
gplt.polyplot(shp, facecolor='lightgray', edgecolor='None', ax=ax)
# 设置标题
plt.title("全国压岁钱分布图")
# 显示图形
plt.show()
cartogram(变形统计图)
import pandas as pd
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mapclassify as mc
scheme = mc.Quantiles(shp['Random'], k=5)
ax = gplt.cartogram(
shp,
scale='Random', limits=(0.75, 1),
projection=gcrs.PlateCarree(),
hue='Random', cmap='Reds', scheme=scheme,
linewidth=0.5,
legend=True, legend_kwargs={'loc': 'lower right'}, legend_var='hue',
figsize=(12, 7)
)
gplt.polyplot(shp, facecolor='lightgray', edgecolor='None', ax=ax)
plt.title("全国压岁钱分布图")
Text(0.5, 1.0, '全国压岁钱分布图')
高清版
import pandas as pd
import geopandas as gpd
import geoplot as gplt
import geoplot.crs as gcrs
import matplotlib.pyplot as plt
import mapclassify as mc
scheme = mc.Quantiles(shp['Random'], k=5)
# 设置画布大小和DPI
fig, ax = plt.subplots(1, 1, figsize=(12, 7), dpi=300, subplot_kw={'projection': gcrs.PlateCarree()})
# 绘制变形统计图
gplt.cartogram(
shp,
scale='Random', limits=(0.75, 1),
projection=gcrs.PlateCarree(),
hue='Random', cmap='Reds', scheme=scheme,
linewidth=0.5,
legend=True, legend_kwargs={'loc': 'lower right'}, legend_var='hue',
ax=ax # 使用之前创建的ax
)
# 添加灰色底色的多边形轮廓
gplt.polyplot(shp, facecolor='lightgray', edgecolor='None', ax=ax)
# 设置标题
plt.title("全国压岁钱分布图")
# 显示图形
plt.show()