以下是一个示例代码,用于在使用matplotlib
和geopandas
绘制地图并设置区域后,当鼠标点击地图上的某个区域时,返回该区域的名称。
首先,确保你已经安装了matplotlib
、geopandas
和descartes
库(descartes
库用于在matplotlib
中绘制地理空间数据)。如果没有安装,可以通过pip install matplotlib geopandas descartes
进行安装。
另外,你需要一份中国省级行政区划的地理空间数据文件(比如 Shapefile 格式),这里假设已经有了一个名为china_provinces.shp
的文件,它包含了中国各省份的几何边界信息。你可以从相关地理数据平台获取这样的文件。
import geopandas as gpd
import pandas as pd
from plotly.subplots import make_subplots
import matplotlib.pyplot as plt
from shapely.geometry import Point
from pypinyin import pinyin, Style
def to_pinyin(text, style=Style.TONE):
return ''.join([i[0] for i in pinyin(text, style=style)])
# 读取中国省份的GeoJSON数据
china_geojson = gpd.read_file('省.shp') # 请确保路径正确,且文件是GeoJSON格式
gdp_data_t = {
"广东": 130518.8,
"江苏": 92595.4,
"山东": 92069.0,
"浙江": 83467.3,
"河南":