使用geopandas绘制中国地图并标注经纬度散点

成图

获取地图的json或shp文件

可从 http://datav.aliyun.com/tools/atlas/index.html 获取
参考:https://blog.csdn.net/hq19940416/article/details/119808424

读取数据 绘制地图

import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
from shapely import geometry
import os

# plt.rcParams['font.sans-serif'] = 'Microsoft YaHei' # 设置字体为微软雅黑
# 使打印输出显示更全
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)

# 读取中国geojson数据
data = gpd.read_file('china.json') 
print(data['name'])
data = data.drop(34)
prd=data
# 绘制地图
fig,ax = plt.subplots(figsize=(8,6))

给地图上色

使用plt.cm.get_cmap获取渐变颜色
colorbar 颜色大全https://blog.csdn.net/Lee_Yu_Rui/article/details/107995652

# 根据省份数量获取对应的颜色映射
colormap = plt.cm.get_cmap('Pastel2_r', 34)

# 获取颜色列表
colors = [colormap(i) for i in range(len(data))]
p = prd.plot(ax=ax,color=colors, scheme="quantiles",edgecolor='grey',alpha=0.8)

设置经纬度范围

# 设置经纬度范围
ax.set_ylim(15,60)
ax.set_xlim(70,140)
# 设置轴标签为经纬度
ax.set_xticks(range(70,145,10),[str(x)+'°' for x in range(70,145,10)],fontsize=14)
ax.set_yticks(range(15,65,10),[str(x)+'°' for x in range(15,65,10)],fontsize=14)


coordinates = [(96.6, 33.2), (88.8, 31.7), (100.24, 34.47)]
sizes = [1259, 761, 879]  # 数据大小
labels = ['Yushu', 'Tibet', 'Golog']  # 标签
colormap = plt.cm.get_cmap('autumn', 3)
# 获取颜色列表
colors = [colormap(i) for i in range(3)]

# 绘制点和标签
scatter_points = []  # 存储绘制的点对象
for coord, size, label,color in zip(coordinates, sizes, labels,colors):
    point = ax.scatter(coord[0], coord[1], s=size/10, color=color)
    scatter_points.append(point)
    ax.annotate(label, (coord[0], coord[1]), textcoords="offset points", xytext=(0,10), ha='center',fontsize=14)

# 创建图例并指定要显示的图例
ax.legend(scatter_points, sizes,fontsize=14)

#设置网格线图例和标题
ax.grid(ls='--',alpha=0.8)

# 保存输出图形
plt.tight_layout()
plt.savefig('china.png',dpi=600)
plt.show()
物联沃分享整理
物联沃-IOTWORD物联网 » 使用geopandas绘制中国地图并标注经纬度散点

发表评论