首页 > 解决方案 > 如何从 matplotlib 返回图例数据

问题描述

我使用 geopandas 从 shapefile 创建了一个数据框,然后使用 gdf.plot 函数进行了绘制。我希望将颜色值分配给以下格式或类似格式的数据类别:

{'1':'black(or color hex code)', 
'2':'color/#hexcode'....,
'7':'color/#hexcode'}

该图是使用以下代码创建的:

plot = geodataframe.plot(
               column='map_c',
               categorical=True,
               legend=True,
               ax=ax,
               cmap='Accent_r')

代码的输出

是否可以从情节中获取此元数据?

标签: pythonmatplotlibgisshapefilegeopandas

解决方案


这是我使用 pylab 发现的一种解决方法

from pylab import cm
from matplotlib.colors import rgb2hex

cmap = cm.get_cmap('Accent_r', 7) #7 categories I had
plot = geodataframe.plot(
               column='map_c',
               categorical=True,
               legend=True,
               ax=ax,
               cmap= cmap)
hexes = [rgb2hex(cmap(i)) for i in range(cmap.N)]

参考这个答案:https ://stackoverflow.com/a/33597599/15408580


推荐阅读