如何在 python geopandas choropleth 地图中为子区域应用不同的边框宽度
How to apply different border widths for subregions in python geopandas choropleth map
我正在用 geopandas 制作等值线图。我想绘制具有两层边界的地图:较薄的边界用于民族国家(geopandas 默认值),而较厚的边界用于各种经济社区。这在 geopandas 中可行吗?
这是一个例子:
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world.query('continent == "Africa"')
EAC = ["KEN", "RWA", "TZA", "UGA", "BDI"]
africa["EAC"] = np.where(np.isin(africa["iso_a3"], EAC), 1, 0)
africa.plot(column="pop_est")
plt.show()
我为属于 EAC 组的国家/地区创建了一个虚拟变量。我想在这个组中的国家周围画一个更粗的边界,同时也留下国家边界。
编辑:
我仍然不知道如何在子图中进行这项工作。这是一个例子:
axs = ["ax1", "ax2"]
vars = ["pop_est", "gdp_md_est"]
fig, axs = plt.subplots(ncols=len(axs),
figsize=(10, 10),
sharex=True,
sharey=True,
constrained_layout=True)
for ax, var in zip(axs, vars):
africa.plot(ax=ax,
column=var,
edgecolor="black",
missing_kwds={
"color": "lightgrey",
"hatch": "///"
})
ax.set_title(var)
plt.show()
我无法直接应用 Martin 的解决方案。
将背景图指定为轴并在第二个图中使用它,仅绘制 EAC 国家。要只有轮廓,您需要 facecolor='none'
.
ax = africa.plot(column="pop_est")
africa.loc[africa['EAC'] == 1].plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)
如果您只想在这些国家/地区周围设置边界,则必须先 dissolve
几何形状。
africa.loc[africa['EAC'] == 1].dissolve('EAC').plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)
我正在用 geopandas 制作等值线图。我想绘制具有两层边界的地图:较薄的边界用于民族国家(geopandas 默认值),而较厚的边界用于各种经济社区。这在 geopandas 中可行吗?
这是一个例子:
import geopandas as gpd
import numpy as np
import matplotlib.pyplot as plt
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
africa = world.query('continent == "Africa"')
EAC = ["KEN", "RWA", "TZA", "UGA", "BDI"]
africa["EAC"] = np.where(np.isin(africa["iso_a3"], EAC), 1, 0)
africa.plot(column="pop_est")
plt.show()
我为属于 EAC 组的国家/地区创建了一个虚拟变量。我想在这个组中的国家周围画一个更粗的边界,同时也留下国家边界。
编辑:
我仍然不知道如何在子图中进行这项工作。这是一个例子:
axs = ["ax1", "ax2"]
vars = ["pop_est", "gdp_md_est"]
fig, axs = plt.subplots(ncols=len(axs),
figsize=(10, 10),
sharex=True,
sharey=True,
constrained_layout=True)
for ax, var in zip(axs, vars):
africa.plot(ax=ax,
column=var,
edgecolor="black",
missing_kwds={
"color": "lightgrey",
"hatch": "///"
})
ax.set_title(var)
plt.show()
我无法直接应用 Martin 的解决方案。
将背景图指定为轴并在第二个图中使用它,仅绘制 EAC 国家。要只有轮廓,您需要 facecolor='none'
.
ax = africa.plot(column="pop_est")
africa.loc[africa['EAC'] == 1].plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)
如果您只想在这些国家/地区周围设置边界,则必须先 dissolve
几何形状。
africa.loc[africa['EAC'] == 1].dissolve('EAC').plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)