怎么用Python制作中国GDP分布图
发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章主要介绍"怎么用Python制作中国GDP分布图",在日常操作中,相信很多人在怎么用Python制作中国GDP分布图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2025年02月07日怎么用Python制作中国GDP分布图
这篇文章主要介绍"怎么用Python制作中国GDP分布图",在日常操作中,相信很多人在怎么用Python制作中国GDP分布图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么用Python制作中国GDP分布图"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
数据读取
## 导入相关模块import pandas as pdimport geopandas as gpdimport numpy as np import matplotlib.pyplot as pltfrom shapely.geometry import Pointimport matplotlib.patches as mpatchesfrom mpl_toolkits.basemap import Basemapfrom matplotlib_scalebar.scalebar import ScaleBar
plt.rcParams['font.family'] = ['sans-serif']plt.rcParams['font.sans-serif'] = ['SimHei']# 替换sans-serif字体为黑体plt.rcParams['axes.unicode_minus'] = False # 解决坐标轴负数的负号显示问题
regibns = gpd.GeoDataFrame.from_file("F:\ArcGIS\ArcGIS文件\全国\中国地图\省级行政区.shp")regibns.plot()
## 查看坐标系regibns.crs
## 绘制国界线nine_lines = gpd.read_file('F:\ArcGIS\ArcGIS文件\全国\中国地图\国界线.shp')nine_lines.plot()
## 经纬网jingwei = gpd.GeoDataFrame.from_file('F:\ArcGIS\ArcGIS文件\全国\中国地图\经纬网.shp')fig = plt.figure(figsize=(8,8)) #设置画布大小ax = plt.gca()regibns.plot(ax=ax)jingwei.plot(ax=ax)nine_lines.plot(ax=ax)
taiwan = gpd.GeoDataFrame.from_file("F:\ArcGIS\ArcGIS文件\全国\中国地图\省级行政区.shp")taiwan = taiwan[regibns['NAME'].isin(['台湾'])]taiwan.plot()
数据清洗
regibns = regibns[['GDP_1994(','GDP_1997(','GDP_1998(','GDP_1999(','GDP_2000(','NAME','geometry']]regibns = regibns.rename(columns={'GDP_1994(':'GDP_1994','GDP_1997(':'GDP_1997','GDP_1998(':'GDP_1998','GDP_1999(':'GDP_1999','GDP_2000(':'GDP_2000'})
regibns.head()
data = pd.read_excel('中国各省GDP.xlsx')data.shape
data.head()
数据连接
GDP = pd.merge(regibns,data,on='NAME') ## 连接GDP.head()
绘图
## 绘制中国2020年GDP分布图fig = plt.figure(figsize=(12,12)) #设置画布大小ax = plt.gca()ax.set_title("中国2020年各省级行政单位GDP分布图(单位:亿元)",fontsize=24,loc='center')regibns['coords'] = regibns['geometry'].apply(lambda x: x.representative_point().coords[0])for n, i in enumerate(regibns['coords']): plt.text(i[0], i[1], regibns['NAME'][n], size=12) # 主图绘制GDP.plot(ax=ax,column='GDP_2020',scheme='quantiles',legend=True,linewidth=0.5,cmap='Reds',edgecolor='k',legend_kwds={ 'loc': 'lower left', 'title': '图例', 'shadow': True, 'fontsize':12, 'frameon':True, 'prop':{'family': 'Times New Roman', 'weight': 'normal', 'size': 12}})jingwei.plot(ax=ax,linewidth=2,alpha=0.5,edgecolor='black')nine_lines.plot(ax=ax,edgecolor='black',linewidth=2,alpha=0.5)taiwan.plot(ax=ax,hatch= "////",label= "缺失值",facecolor='lightgrey')# 副图框绘制ax_child = fig.add_axes([0.72, 0.20, 0.15, 0.15]) # left, bottom, width, heightGDP.plot(ax=ax_child,color='#E24A33',edgecolor='grey',linewidth=0.5)GDP.plot(ax=ax_child,color='#348ABD',edgecolor='grey',linewidth=0.5)ax_child = nine_lines.geometry.plot(ax=ax_child,edgecolor='black',linewidth=2,alpha=0.5)taiwan.plot(ax=ax_child,hatch= "////",label= "缺失值",facecolor='lightgrey')ax_child.set(xlim=(0*10**6,2*10**6),ylim=(0*10**6,2.8*10**6))ax_child.set_xticks([])ax_child.set_yticks([])# 额外图例绘制p1=gpd.GeoDataFrame({'geometry':[Point(-1.60*10**6,-0.1*10**6)]})p1.plot(ax=ax,markersize=100,facecolor='lightgrey',hatch= "////")ax.text(-1.54*10**6,-0.15*10**6, "NoData",{'family': 'Times New Roman', 'weight': 'normal', 'size': 12})# 添加比例尺scalebar = ScaleBar(dx=1*10**-3,units='km',length_fraction=0.1, font_properties={'family': 'Times New Roman', 'weight': 'normal', 'size': 12}, location=8,sep=1,frameon=False)ax.add_artist(scalebar)# 添加指北针x, y, arrow_length = 0.42, 0.09, 0.07ax.annotate('N', xy=(x, y), xytext=(x, y-arrow_length), arrowprops=dict(facecolor='black', width=4, headwidth=7), ha='center', va='center', fontsize=10, xycoords=ax.transAxes)# plt.savefig('中国2020年各省级行政单位GDP分布图.png',dpi=300)
批量出图
为了出图方便、可控、美观,所以有所简略。
reg = GDP.copy() ##好习惯,数据不干扰
## 列表表达式data_plot = [('GDP_1994','中国1994年GDP分布图(单位:亿元)'),('GDP_1997','中国1997年GDP分布图(单位:亿元)'),('GDP_1998','中国1998年GDP分布图(单位:亿元)'),('GDP_1999','中国1999年GDP分布图(单位:亿元)'),('GDP_2000','中国2000年GDP分布图(单位:亿元)'),('GDP_2010','中国2010年GDP分布图(单位:亿元)'),('GDP_2019','中国2019年GDP分布图(单位:亿元)'),('GDP_2020','中国2020年GDP分布图(单位:亿元)'),]
## 批量出图,不要忘记台湾for m, cal in enumerate(data_plot): reg['coords'] = reg['geometry'].apply(lambda x: x.representative_point().coords[0]) ax = reg.plot(figsize=(10, 10), column=cal[0], scheme='quantiles', legend=True, cmap='Reds', edgecolor='k') taiwan.plot(ax=ax,hatch= "////",label= "缺失值",facecolor='lightgrey') for n, i in enumerate(regibns['coords']): plt.text(i[0], i[1], regibns['NAME'][n], size=12) plt.title(cal[1],size=20) plt.grid(True, alpha=0.3) # plt.savefig(str(m)+'.png',dpi=300)
## 绘制子图fig=plt.figure(figsize=(15,30))for m, cal in enumerate(data_plot): reg['coords'] = reg['geometry'].apply(lambda x: x.representative_point().coords[0]) ax = reg.plot(ax=plt.subplot(4,2,m+1),column=cal[0],figsize = (10,10), scheme='quantiles', legend=True, cmap='Reds', edgecolor='k') taiwan.plot(ax=ax,hatch= "////",label= "缺失值",facecolor='lightgrey') for n, i in enumerate(regibns['coords']): plt.text(i[0], i[1], regibns['NAME'][n], size=12) #plt.subplots_adjust(bottom=0.1, right=0.6, top=0.5) plt.title(cal[1],size=20) plt.grid(True, alpha=0.3)plt.savefig('中国GDP演变图2.png',dpi=300)
制作动图
## 创建文件夹夹def mkdir(path): folder = os.path.exists(path) if not folder: # 判断是否存在文件夹如果不存在则创建为文件夹 os.makedirs(path) # makedirs 创建文件时如果路径不存在会创建这个路径 print("--- new folder... ---") print("--- OK ---") else: print("--- There is this folder! ---")file = r'F:\ArcGIS\ArcGIS文件\全国\中国地图ArcGIS练习数据\代码\photo'mkdir(file) # 调用函数
## 复制文件import shutilarray = np.arange(9)ls=list(array)rs=map(str,ls)path=r'F:\ArcGIS\ArcGIS文件\全国\中国地图ArcGIS练习数据\代码' #待读取的文件夹root = 'F:\ArcGIS\ArcGIS文件\全国\中国地图ArcGIS练习数据\代码\photo'num=0for num in range(8): name = str(num) + '.png'#将0-8选出来 if name in os.listdir(path):#取出文件名数字部分是18倍数的文件 sourcefile = os.path.join(path, name) # 拼路径 print(sourcefile) shutil.copy(sourcefile, root)# 将指定的文件复制到root的文件夹里面 else: print("no")
## 制作动图# _*_ coding:utf-8 _*_import matplotlib.pyplot as pltimport imageiofrom PIL import Image, ImageSequence__author__ = 'admin'GIF=[]filepath = r'F:\ArcGIS\ArcGIS文件\全国\中国地图ArcGIS练习数据\代码\photo'#文件路径filenames=os.listdir(filepath)for filename in os.listdir(filepath): GIF.append(imageio.imread(filepath+"\\"+filename))imageio.mimsave(filepath+"\\"+'result.gif',GIF,duration=1)#这个duration是播放速度,数值越小,速度越快
到此,关于"怎么用Python制作中国GDP分布图"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
中国
文件
分布图
单位
中国地图
全国
地图
数据
制作
文件夹
代码
缺失
路径
学习
出图
国界
国界线
图例
坐标
大小
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
黑龙江省网络安全实验室
首都师范大学数据库应用与技术
软件开发应该裸辞吗
网络安全绘画八年级
池州医疗软件开发定制
企业单位网络安全运维管理
华为服务器不识别ntfs格式
山东华为服务器虚拟化多少钱
如何查一款软件开发商
用友数据库实例
网络安全教育教案教后感
遵化网络安全会议
网络安全警示教育片部队新闻
棋牌数据库异常启动失败
数据库实现分页查询数据库
平台棋牌软件开发
文明行为网络安全手抄报
卸载uos数据库
公司软件开发部门人员构成
仓库管理系统要买云服务器吗
海康服务器的硬盘如何取出
公司软件开发师徒模式
网络安全四个坚决
使用的屏幕共享软件开发
inchi数据库
女生做软件开发劣势
数学建模和数据库
网络数据库技术分析
mysql电脑上怎么备份数据库
怎样确保数据库安全