python - 如何从 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')
是否可以从情节中获取此元数据?
解决方案
这是我使用 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)]
推荐阅读
- android - 当设备有切口(缺口)时,有什么方法可以拉伸布局?
- python - else & elif 语句似乎在 Python 中不起作用
- javascript - 多个可拖动元素
- c++ - 如何在 WebView2 中设置基于选项卡的 cookie?
- javascript - 循环遍历 div 并添加 onmouseover
- python - model.evaluate() 和 model.predict() 返回非常不同的结果
- amazon-web-services - 如果联合帐户在用户池中是单独的,则使用 Cognito 联合链接帐户登录时出现内部服务器错误
- sql - 有没有办法可以识别模块中以不同方式使用的表?
- python - tkinter 中的可滚动框架类
- android - 假设 Github 会保留来自 Android Studio 的数据库中的数据是错误的吗?