Matplotlib 等值线图绘制两个不同的数据
Matplotlib choropleth map plotting two different pieces of data
到目前为止,我已经通过以下方式制作了 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.')
看起来像这样:
我的 DF 是这样的:
geometry Counts WardCode Ward Name Income Rank
POLYGON (()) 1545 N09000001 Abbey 3
所以它绘制了每个区域与我在 df 中的收入数据相关的排名。我也可以在这张地图上策划犯罪吗?我试图显示低收入和高犯罪率之间的 link。例如,用标记或可能使用不同的配色方案来代表高犯罪率地区?包含我的罪行的数据框如下所示:
WARDNAME Counts
0 CENTRAL 3206
1 DUNCAIRN 757
2 BLACKSTAFF 584
我也有一个带有纬度和经度的犯罪 df,看起来像这样:
Crime ID Date Longit Latit Crime type Ward Name Ward Code
0 01 2016-01 -5.886699 54.591309 Theft CENTRAL N08000313
我可以通过使用 Folium 并用收入值绘制 Choropleth 然后绘制犯罪作为标记来在同一张地图上绘制这两个东西的唯一方法吗?或者我可以在没有叶的情况下做吗?
谢谢
对于具有 2 个多边形叠加层的等值线图,您需要在顶层使用(半或)透明图。让我用这个例子来证明。您需要将 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()
结果图:
到目前为止,我已经通过以下方式制作了 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.')
看起来像这样:
我的 DF 是这样的:
geometry Counts WardCode Ward Name Income Rank
POLYGON (()) 1545 N09000001 Abbey 3
所以它绘制了每个区域与我在 df 中的收入数据相关的排名。我也可以在这张地图上策划犯罪吗?我试图显示低收入和高犯罪率之间的 link。例如,用标记或可能使用不同的配色方案来代表高犯罪率地区?包含我的罪行的数据框如下所示:
WARDNAME Counts
0 CENTRAL 3206
1 DUNCAIRN 757
2 BLACKSTAFF 584
我也有一个带有纬度和经度的犯罪 df,看起来像这样:
Crime ID Date Longit Latit Crime type Ward Name Ward Code
0 01 2016-01 -5.886699 54.591309 Theft CENTRAL N08000313
我可以通过使用 Folium 并用收入值绘制 Choropleth 然后绘制犯罪作为标记来在同一张地图上绘制这两个东西的唯一方法吗?或者我可以在没有叶的情况下做吗? 谢谢
对于具有 2 个多边形叠加层的等值线图,您需要在顶层使用(半或)透明图。让我用这个例子来证明。您需要将 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()
结果图: