如何以地图的形式为子地块提供统一的颜色比例?

2024-04-18 08:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用geopandas在韩国地图上绘制了2021年至2030年年度煤炭淘汰数据的区域分布图。它看起来如下:enter image description here

一个问题是,每年或子批次的色标不同,因为它们基于数据框的不同列,如图enter image description here

我想有一个所有年份的统一色标,并插入一个单一的图例。如何为每个子地块(代表每年)提供统一的色标,如何为所有子地块插入通用图例


Tags: 数据区域地图分布图绘制代表geopandas年份
1条回答
网友
1楼 · 发布于 2024-04-18 08:34:41

对于公共颜色映射,需要使用vminvmax关键字参数。首先,让我们计算它们

vmin = min(df[year].min() for year in range(2021, 2027))
vmax = max(df[year].max() for year in range(2021, 2027))

并修改所有绘图,如下所示:

south_korea.plot(2021, cmap="Reds", ax=axs[0, 0], vmin=vmin, vmax=vmax)

现在,让我们为它们添加颜色栏。我用Colorbar on Geopandas来学习如何做

cax = fig.add_axes([0.9, 0.1, 0.03, 0.8])
sm = plt.cm.ScalarMappable(cmap='Reds', norm=plt.Normalize(vmin=vmin, vmax=vmax))
# fake up the array of the scalar mappable. Urgh...
sm._A = []
fig.colorbar(sm, cax=cax)

请注意,geopandas使用matplotlib的许多功能,因此您可以更轻松地传递知识和搜索

相关问题 更多 >