Matplotlib Choropleth地图绘制两个不同的数据

时间:2018-11-19 19:24:13

标签: python pandas matplotlib geopandas choropleth

到目前为止,我已经通过以下方式制作了Choropleth地图:

fig, ax = plt.subplots(1, figsize=(32, 16))
ax.axis('off')

df.plot(column='Income Rank', scheme='quantiles', k=7,legend=True, cmap='YlOrRd_r', ax=ax)
ax.annotate(xy=(0.1, .08),  xycoords='figure fraction', horizontalalignment='left', verticalalignment='top'
            ,s='Income deprivation Rank. Lowest rank = most deprived.')

如下所示: enter image description here

我的DF看起来像这样:

geometry    Counts  WardCode Ward Name   Income Rank                                                                
POLYGON (())    1545    N09000001   Abbey   3

因此,它根据df中的收入数据绘制了每个区域的等级。我也可以在这张地图上绘制犯罪吗?我试图显示低收入和高犯罪率之间的联系。例如,使用标记或使用其他颜色方案代表高犯罪率区域?与我的罪行有关的数据框如下所示:

WARDNAME    Counts
0   CENTRAL 3206
1   DUNCAIRN    757
2   BLACKSTAFF  584

我还有一个包含纬度和经度的犯罪记录,如下所示:

Crime ID    Date    Longit  Latit   Crime type  Ward Name   Ward Code
0   01  2016-01 -5.886699   54.591309  Theft    CENTRAL N08000313

使用Folium并用收入值绘制Choropleth,然后将犯罪绘制为标记,这是我可以在同一张地图上绘制这两种东西的唯一方法吗?或者我可以不用叶来做吗? 谢谢

1 个答案:

答案 0 :(得分:1)

对于具有2个多边形叠加层的Choropleth贴图,您需要在顶层使用(半或)透明图。让我用这个例子演示。您需要安装geopandas才能运行此操作。

import geopandas as gpd
import matplotlib.pyplot as plt

# load world data (provided with geopandas)
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

# select some countries (for top layer plots)
brazil = world[world.name == u'Brazil']
russia = world[world.name == u'Russia']

# grouping countries by continents for base layer
world = world[['continent', 'geometry']]  # for grouping purposes, take 2 columns
continents = world.dissolve(by='continent')  # only column 'geometry' is retained; no aggregated attribute

# plot world's continents first as base layer
ax1 = continents.plot(figsize=(12, 8), cmap='Set2')

# plot other polygons on top of the base layer
# 'facecolor' = 'None' specifies transparent area
# plot Brazil at upper level (zorder=11) using 'hatch' as symbol
# higher value of zorder causes the plot on top of layers with lower values
kwarg3s = {'facecolor': 'None', 'edgecolor': 'green', 'linewidth': 1.5, 'hatch': '|||'}
brazil.plot(zorder=11, ax=ax1, **kwarg3s)

# plot Russia at upper level using 'hatch' as symbol
kwarg4s = {'facecolor': 'None', 'edgecolor': 'red', 'linewidth': 0.5, 'hatch': 'xx'}
russia.plot(zorder=11, ax=ax1, **kwarg4s)
plt.show()

结果图: enter image description here