【地理信息可视化】basemap(cartopy)+geopandas显示地图-02

复制代码
import warnings
warnings.filterwarnings('ignore')

import os

import numpy as np
from scipy.stats import norm
import pandas as pd

import seaborn as sns
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False 

import cartopy.crs as ccrs
import geopandas as gpd
import cartopy.io.shapereader as shpreader
from shapely.geometry import Polygon
from matplotlib.patches import Polygon as mpl_polygon

甲状腺结节数据

复制代码
res={}
result= pd.DataFrame(res)
result.head()

| | 地区1 | 是否异常 | 总人数 | 检出率 |
| 0 | 云南 | 8 | 18 | 44.44 |
| 1 | 内蒙古 | 9 | 18 | 50.00 |
| 2 | 北京 | 6 | 18 | 33.33 |
| 3 | 吉林 | 18 | 18 | 100.00 |

4 四川 9 18 50.00

加载边界数据

复制代码
country_CN = gpd.read_file('ChinaAdminDivisonSHP/1. Country/country.shp', encoding='utf-8')  # 用高德最新API,国家
province_CN = gpd.read_file('ChinaAdminDivisonSHP/2. Province/province.shp', encoding='utf-8') # 省份
nine_dotted_line = gpd.read_file('ChinaAdminDivisonSHP/5. NineDot/china_nine_dotted_line.shp', encoding='utf-8') # 九段线
df_pcenter = pd.read_csv('ChinaAdminDivisonSHP/省市中心坐标位置LBC各省市中心.csv', encoding='gbk') # 区域中心

颜色映射

复制代码
from matplotlib.colors import Normalize, rgb2hex
from matplotlib.cm import ScalarMappable
vmin=result['检出率'].min()
vmax=result['检出率'].max()
# 创建 Normalize 对象,用于将数据映射到 [0, 1] 范围
norm = Normalize(vmin=vmin, vmax=vmax)
# 创建 ScalarMappable 对象,用于将归一化后的数据映射到颜色映射中,注意左侧0,右侧1,色板:
# https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
sm = ScalarMappable(cmap='OrRd', norm=norm)
# 将数据映射为颜色数组
colors = sm.to_rgba(result['检出率'])
# 将颜色数组转换为十六进制格式的颜色数组
hex_colors = [rgb2hex(color) for color in colors]

绘图

复制代码
result['hex_colors']=hex_colors
result['geometry'] = [province_CN[province_CN['pr_name'].str.contains(p)]['geometry'] for p in result['地区1'].tolist()]
result.head()

| | 地区1 | 是否异常 | 总人数 | 检出率 | hex_colors | geometry |
| 0 | 云南 | 8 | 18 | 44.44 | #fdbb85 | 24 POLYGON ((97.72048 24.96677, 97.71953 24... |
| 1 | 内蒙古 | 9 | 18 | 50.00 | #fca36e | 4 POLYGON ((124.52522 48.14594, 124.52310 4... |
| 2 | 北京 | 6 | 18 | 33.33 | #fdd49f | 0 POLYGON ((116.77565 40.02168, 116.77456 4... |
| 3 | 吉林 | 18 | 18 | 100.00 | #7f0000 | 6 POLYGON ((129.59213 42.44687, 129.59630 4... |

4 四川 9 18 50.00 #fca36e 22 MULTIPOLYGON (((107.05537 30.04027, 107....
复制代码
# 创建地图投影
crs = ccrs.LambertConformal(central_longitude=105, central_latitude=35, standard_parallels=(30, 60))
# 创建子图
fig, ax = plt.subplots(figsize=(16, 9), subplot_kw={'projection': crs})
# 绘制国界
ax.add_geometries(country_CN['geometry'], crs=ccrs.PlateCarree(), edgecolor='black', facecolor='none')
# 绘制九段线
ax.add_geometries(nine_dotted_line['geometry'], crs=ccrs.PlateCarree(), edgecolor='black', facecolor='none')
# 绘制各省颜色
for i in range(result.shape[0]):
    province = result['地区1'].iloc[i]
    num = result['检出率'].iloc[i]
    colors_i = result['hex_colors'].iloc[i]
    geometry = result['geometry'].iloc[i]
    ax.add_geometries(geometry, crs=ccrs.PlateCarree(), edgecolor=colors_i, facecolor=colors_i)
    lat = float(df_pcenter[df_pcenter['所在省'].str.contains(province)].iloc[0]['纬度'])
    lon = float(df_pcenter[df_pcenter['所在省'].str.contains(province)].iloc[0]['经度'])
    ax.text(lon, lat, str(num), transform=ccrs.PlateCarree(), fontsize=12, color='black')
# 标注北京
im =  ax.scatter(116.46, 39.92, transform=ccrs.PlateCarree(), s=200, color='red', marker='*')
# 添加色值条
im.set_cmap(cmap='OrRd')
# 显示右侧渐变色值条
cbar = fig.colorbar(im, ax=ax)
# 设置色值条范围和标签
cbar.set_ticks([0,0.5, 1])  # 设置色值条刻度位置
cbar.set_ticklabels([int(vmin),int((vmin+vmax)/2), int(vmax)])  # 设置刻度标签
# 色值条
cbar.ax.set_ylabel('色值条', rotation=270, labelpad=10)
# 调整标签字体大小
cbar.ax.yaxis.label.set_fontsize(12)

# 设置地图范围
ax.set_extent([73, 135, 12, 53], crs=ccrs.PlateCarree())
# 设置标题
ax.set_title('中国各省某数据检出率')
# 显示图像
plt.show()
相关推荐
曲幽3 小时前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
荣码3 小时前
用Streamlit给AI应用套个界面,10行代码出Web页面
java·python
兵慌码乱13 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵14 小时前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio18 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
用户03321266636719 小时前
使用 Python 从零创建 Word 文档
python
Csvn1 天前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽1 天前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户556918817531 天前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
兵慌码乱2 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2