您好,登錄后才能下訂單哦!
這篇文章給出了如何繪制中國(guó)人口密度圖,但是運(yùn)行存在一些問題,我在一些地方進(jìn)行了修改。
本人使用的IDE是anaconda,因此事先在anaconda prompt 中安裝Basemap包
conda install Basemap
新建文檔,導(dǎo)入需要的包
import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from matplotlib.patches import Polygon from matplotlib.colors import rgb2hex import numpy as np import pandas as pd
Basemap中不包括中國(guó)省界,需要在下面網(wǎng)站下載中國(guó)省界,點(diǎn)擊Shapefile下載。
生成中國(guó)大陸省界圖片。
plt.figure(figsize=(16,8)) m = Basemap( llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51, projection='lcc', lat_1=33, lat_2=45, lon_0=100 ) m.drawcountries(linewidth=1.5) m.drawcoastlines() m.readshapefile('gadm36_CHN_shp/gadm36_CHN_1', 'states', drawbounds=True)
去國(guó)家統(tǒng)計(jì)局網(wǎng)站下載人口各省,只需保留地區(qū)和總?cè)丝诩纯?,保存為csv格式并改名為pop.csv。
讀取數(shù)據(jù),儲(chǔ)存為dataframe格式,刪去地名之中的空格,并設(shè)置地名為dataframe的index。
df = pd.read_csv('pop.csv') new_index_list = [] for i in df["地區(qū)"]: i = i.replace(" ","") new_index_list.append(i) new_index = {"region": new_index_list} new_index = pd.DataFrame(new_index) df = pd.concat([df,new_index], axis=1) df = df.drop(["地區(qū)"], axis=1) df.set_index("region", inplace=True)
將Basemap中的地區(qū)與我們下載的csv中的人口數(shù)據(jù)對(duì)應(yīng)起來,建立字典。注意,Basemap中的地名與csv文件中的地名并不完全一樣,需要進(jìn)行一些處理。
provinces = m.states_info statenames=[] colors = {} cmap = plt.cm.YlOrRd vmax = 100000000 vmin = 3000000 for each_province in provinces: province_name = each_province['NL_NAME_1'] p = province_name.split('|') if len(p) > 1: s = p[1] else: s = p[0] s = s[:2] if s == '黑龍': s = '黑龍江' if s == '內(nèi)蒙': s = '內(nèi)蒙古' statenames.append(s) pop = df['人口數(shù)'][s] colors[s] = cmap(np.sqrt((pop - vmin) / (vmax - vmin)))[:3]
最后畫出圖片即可
ax = plt.gca() for nshape, seg in enumerate(m.states): color = rgb2hex(colors[statenames[nshape]]) poly = Polygon(seg, facecolor=color, edgecolor=color) ax.add_patch(poly) plt.show()
完整代碼如下
# -*- coding: utf-8 -*- import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from matplotlib.patches import Polygon from matplotlib.colors import rgb2hex import numpy as np import pandas as pd plt.figure(figsize=(16,8)) m = Basemap( llcrnrlon=77, llcrnrlat=14, urcrnrlon=140, urcrnrlat=51, projection='lcc', lat_1=33, lat_2=45, lon_0=100 ) m.drawcountries(linewidth=1.5) m.drawcoastlines() m.readshapefile('gadm36_CHN_shp/gadm36_CHN_1', 'states', drawbounds=True) df = pd.read_csv('pop.csv') new_index_list = [] for i in df["地區(qū)"]: i = i.replace(" ","") new_index_list.append(i) new_index = {"region": new_index_list} new_index = pd.DataFrame(new_index) df = pd.concat([df,new_index], axis=1) df = df.drop(["地區(qū)"], axis=1) df.set_index("region", inplace=True) provinces = m.states_info statenames=[] colors = {} cmap = plt.cm.YlOrRd vmax = 100000000 vmin = 3000000 for each_province in provinces: province_name = each_province['NL_NAME_1'] p = province_name.split('|') if len(p) > 1: s = p[1] else: s = p[0] s = s[:2] if s == '黑龍': s = '黑龍江' if s == '內(nèi)蒙': s = '內(nèi)蒙古' statenames.append(s) pop = df['人口數(shù)'][s] colors[s] = cmap(np.sqrt((pop - vmin) / (vmax - vmin)))[:3] ax = plt.gca() for nshape, seg in enumerate(m.states): color = rgb2hex(colors[statenames[nshape]]) poly = Polygon(seg, facecolor=color, edgecolor=color) ax.add_patch(poly) plt.show()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。