使用geopandas和matplotlib绘制动态地图

使用geopandas和matplotlib绘制动态地图

本文是Let’s make a map! Using Geopandas, Pandas and Matplotlib to make a Choropleth mapHow to make a gif map using Python, Geopandas and Matplotlib两篇文章的学习笔记。

准备数据

本文需要的数据有三份,伦敦的shp地理数据集、伦敦人口死亡率数据和犯罪率数据。
LondonDatasets.zip
london-borough-profile.csv
MPS_Borough_Level_Crime_Historic.csv

绘制伦敦地图

1
2
3
4
5
6
7
8
import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt
fp = "LondonDatasets/London_Borough_Excluding_MHW.shp"
map_df = gpd.read_file(fp)
map_df.head()
map_df.plot()
plt.savefig("伦敦地图.png")

伦敦死亡率分布图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 下面读入csv数据
df = pd.read_csv("london-borough-profile.csv")
df.head()
# 只保留需要的几列
df = df[['borough', \
'Happiness_score_2011-14_(out_of_10)', \
'Anxiety_score_2011-14_(out_of_10)', \
'Population_density_(per_hectare)_2017', \
'Mortality_rate_from_causes_considered_preventable_2012/14']]
# 列重命名
data_for_map = df.rename(index = str, columns = \
{"Happiness_score_2011-14_(out_of_10)": "幸福感", \
"Anxiety_score_2011-14_(out_of_10)": "焦虑感", \
"Population_density_(per_hectare)_2017": "人口密度", \
"Mortality_rate_from_causes_considered_preventable_2012/14": "死亡率", \
"borough": "自治市"})
data_for_map.head()

# 合并两个数据集
merged = map_df.set_index('NAME').join(data_for_map.set_index('自治市'))

# 绘制地图
# 设定绘图变量
variable = '人口密度'

# 设定等值线的范围
vmin, vmax = 120, 220

# 创建图片和坐标轴
fig, ax = plt.subplots(1, figsize = (10, 6))

# 创建地图
merged.plot(column = variable, cmap = 'Blues', \
linewidth = 0.8, ax = ax, \
edgecolor = '0.8')

plt.savefig("伦敦人口密度图.png")

# 移除坐标轴
ax.axis('off')

# 添加标题
ax.set_title('图:伦敦的可预防死亡率分布', \
fontdict = {'fontsize': '25', 'fontweight': '3'})

# 添加注释
ax.annotate('数据来源: London Datastore, 2014', \
xy = (0.1, 0.08), \
xycoords = 'figure fraction', \
horizontalalignment = 'left', verticalalignment = 'top', \
fontsize = 12, color = '#555555')
# 创建图例
sm = plt.cm.ScalarMappable(cmap = 'Blues', \
norm = plt.Normalize(vmin = vmin, vmax = vmax))
sm._A = []
cbar = fig.colorbar(sm)
plt.savefig("伦敦的可预防死亡率分布.png")

犯罪率GIF图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 创建动态地图
output_path = 'charts/'
i = 0
list_of_years = ['200807', '200907', '201007', \
'201107', '201207', '201307', \
'201407', '201507', '201607']
vmin, vmax = 0, 120
df2 = pd.read_csv('MPS_Borough_Level_Crime_Historic.csv')
merged1 = map_df.set_index('NAME').join(df2.set_index('borough'))
for year in list_of_years:
fig = merged1.plot(column = year, cmap = 'Blues', \
figsize = (10, 10), linewidth = 0.8, \
edgecolor = '0.8', vmin = vmin, \
vmax = vmax, legend = True, \
norm = plt.Normalize(vmin = vmin,
vmax = vmax))
fig.axis('off')
fig.set_title('图:伦敦的暴力犯罪分布', \
fontdict = {'fontsize': '25', \
'fontweight': '3'})
only_year = year[:4]
fig.annotate(only_year, \
xy = (0.1, 0.225), \
xycoords = 'figure fraction', \
horizontalalignment = 'left', \
verticalalignment = 'top', \
fontsize = 35)
filepath = os.path.join(output_path, only_year + '_violence.png')
chart = fig.get_figure()
chart.savefig(filepath)

# 然后使用convert命令将这些png图片合成GIF图即可。

# Python

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×