Skip to main content
 首页 » 编程设计

python之Matplotlib 等值线图绘制两个不同的数据

2024年12月31日10yyy_WW

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

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 中的收入数据相关的排名。我也可以在这张 map 上策划犯罪吗?我试图展示低收入和高犯罪率之间的联系。例如,用标记或者可能使用不同的配色方案来代表高犯罪率地区?包含我的罪行的数据框如下所示:

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 然后绘制犯罪作为标记来在同一张 map 上绘制这两个东西的唯一方法吗?或者我可以在没有叶的情况下做吗? 谢谢

请您参考如下方法:

对于具有 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() 

结果图: