怎么用Python制作中國GDP分布圖

這篇文章主要介紹“怎么用Python制作中國GDP分布圖”,在日常操作中,相信很多人在怎么用Python制作中國GDP分布圖問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用Python制作中國GDP分布圖”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)是一家專注于網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計,北關(guān)網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:北關(guān)等地區(qū)。北關(guān)做網(wǎng)站價格咨詢:18980820575

數(shù)據(jù)讀取

## 導(dǎo)入相關(guān)模塊
import pandas as pd
import geopandas as gpd
import numpy as np 
import matplotlib.pyplot as plt
from shapely.geometry import Point
import matplotlib.patches as mpatches
from mpl_toolkits.basemap import Basemap
from 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   # 解決坐標(biāo)軸負(fù)數(shù)的負(fù)號顯示問題
regibns = gpd.GeoDataFrame.from_file("F:\ArcGIS\ArcGIS文件\全國\中國地圖\省級行政區(qū).shp")
regibns.plot()
## 查看坐標(biāo)系
regibns.crs

怎么用Python制作中國GDP分布圖

## 繪制國界線
nine_lines = gpd.read_file('F:\ArcGIS\ArcGIS文件\全國\中國地圖\國界線.shp')
nine_lines.plot()

怎么用Python制作中國GDP分布圖

## 經(jīng)緯網(wǎng)
jingwei = gpd.GeoDataFrame.from_file('F:\ArcGIS\ArcGIS文件\全國\中國地圖\經(jīng)緯網(wǎng).shp')
fig = plt.figure(figsize=(8,8)) #設(shè)置畫布大小
ax = plt.gca()
regibns.plot(ax=ax)
jingwei.plot(ax=ax)
nine_lines.plot(ax=ax)

怎么用Python制作中國GDP分布圖

taiwan = gpd.GeoDataFrame.from_file("F:\ArcGIS\ArcGIS文件\全國\中國地圖\省級行政區(qū).shp")
taiwan = taiwan[regibns['NAME'].isin(['臺灣'])]
taiwan.plot()

怎么用Python制作中國GDP分布圖

數(shù)據(jù)清洗

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()

怎么用Python制作中國GDP分布圖

data = pd.read_excel('中國各省GDP.xlsx')
data.shape

怎么用Python制作中國GDP分布圖

data.head()

怎么用Python制作中國GDP分布圖

數(shù)據(jù)連接

GDP = pd.merge(regibns,data,on='NAME') ## 連接
GDP.head()

怎么用Python制作中國GDP分布圖

繪圖

## 繪制中國2020年GDP分布圖
fig = plt.figure(figsize=(12,12)) #設(shè)置畫布大小
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, height

GDP.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.07
ax.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)

怎么用Python制作中國GDP分布圖

批量出圖

為了出圖方便、可控、美觀,所以有所簡略。

reg = GDP.copy() ##好習(xí)慣,數(shù)據(jù)不干擾
## 列表表達(dá)式
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)

怎么用Python制作中國GDP分布圖

怎么用Python制作中國GDP分布圖

## 繪制子圖
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)

怎么用Python制作中國GDP分布圖

制作動圖

## 創(chuàng)建文件夾夾
def mkdir(path):
    folder = os.path.exists(path)
    if not folder:  # 判斷是否存在文件夾如果不存在則創(chuàng)建為文件夾
        os.makedirs(path)  # makedirs 創(chuàng)建文件時如果路徑不存在會創(chuàng)建這個路徑
        print("---  new folder...  ---")
        print("---  OK  ---")

    else:
        print("---  There is this folder!  ---")

file = r'F:\ArcGIS\ArcGIS文件\全國\中國地圖ArcGIS練習(xí)數(shù)據(jù)\代碼\photo'
mkdir(file)  # 調(diào)用函數(shù)
## 復(fù)制文件
import shutil
array = np.arange(9)
ls=list(array)
rs=map(str,ls)
path=r'F:\ArcGIS\ArcGIS文件\全國\中國地圖ArcGIS練習(xí)數(shù)據(jù)\代碼'  #待讀取的文件夾
root = 'F:\ArcGIS\ArcGIS文件\全國\中國地圖ArcGIS練習(xí)數(shù)據(jù)\代碼\photo'


num=0

for num in range(8):
    name = str(num) + '.png'#將0-8選出來
    if name in os.listdir(path):#取出文件名數(shù)字部分是18倍數(shù)的文件
        sourcefile = os.path.join(path, name)  # 拼路徑
        print(sourcefile)
        shutil.copy(sourcefile, root)# 將指定的文件復(fù)制到root的文件夾里面
    else:
        print("no")

怎么用Python制作中國GDP分布圖

## 制作動圖
#   _*_ coding:utf-8 _*_
import matplotlib.pyplot as plt
import imageio
from PIL import Image, ImageSequence
__author__ = 'admin'


GIF=[]
filepath = r'F:\ArcGIS\ArcGIS文件\全國\中國地圖ArcGIS練習(xí)數(shù)據(jù)\代碼\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是播放速度,數(shù)值越小,速度越快

怎么用Python制作中國GDP分布圖

到此,關(guān)于“怎么用Python制作中國GDP分布圖”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

網(wǎng)頁題目:怎么用Python制作中國GDP分布圖
文章分享:http://muchs.cn/article22/jopjcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、做網(wǎng)站、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、Google、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名