【发布时间】:2021-05-29 04:52:55
【问题描述】:
我有一个 shapefile,它有一个属性表,其中有一列我想制作地图/绘图。属性值是数字(整数)。我制作了两个字典来将我想要的颜色和名称映射到这些整数。
Palette = {0: 'black',
20: '#FFBB22',
30: '#FFFF4C',
40: '#F096FF',
80: '#0032C8',
90: '#0096A0',
112: '#009900',
114: '#00CC00',
116: '#007800',
124: '#A0DC00',
126:'#648C00'}
names = {0: 'NAN',
20: 'Shrubs',
30: 'Herbaceous',
40: 'Cultivated',
80: 'Permanent Water',
90: 'Herbaceous Wetland',
112: 'Closed Forest: Evergreen',
114: 'Closed Forest: Deciduous broad leaf',
116: 'Closed forest: Other',
124: 'Open forest: Deciduous broad leaf',
126:'Open forest: Other'}
但是,虽然我可以将颜色映射到正确的值,但我无法让图例显示正确的名称。图例显示为空,我收到一条消息“找不到带有标签的句柄放入图例”
我的代码是:
fig, ax = plt.subplots(figsize=(5, 5))
# Loop through each attribute value and assign each
# with the correct color & width specified in the dictionary
for ctype, data in map_df.groupby('landcovermode'):
color = Palette[ctype]
label = names[ctype]
data.plot(color=color,
ax=ax,
label=label,legend=True)
# Place legend in the lower right hand corner of the plot
ax.legend(loc='lower right',
fontsize=15,
frameon=True)
ax.set_axis_off()
如何让图例从字典中读取我的标签?
【问题讨论】:
标签: python matplotlib legend geopandas