【发布时间】:2022-01-05 10:13:18
【问题描述】:
我有以下情节:
在这张地图中,您会看到一些带有数字的随机颜色区域。数字是 12、25、34、38 和 43。现在我想在左上角添加一个图例,数字后跟区域名称。像这样的:
使用以下命令 ax.annotate(number, xy = ...) 通过 for 循环添加注释(区域中的数字)。有人可以告诉我如何在类似于上图的某种图例中添加包含所有数字和文本的图例吗?
数字和名称都在 pandas 数据框中。
fig, ax = plt.subplots(1,1)
fig.set_size_inches(8,8) # setting the size
# Plot values - with grey layout
grey_plot = schap.plot(ax = ax, color = 'grey')
schap.plot(ax = grey_plot, column= col1, cmap= 'YlGnBu', legend = True)
# Add annotation for every waterschap with a deelstroomgebied
bbox_props = dict(boxstyle="round", fc="w", ec="gray", alpha=0.9,lw=0.4)
for idx, row in schap.iterrows():
if not np.isnan(row[col1]):
string = str(idx)
ax.annotate(string, xy=row['coords'], color='black',
horizontalalignment='center', bbox=bbox_props, fontsize=7)
'schap' 是包含所有需要数据的熊猫数据框。 schap['text'] 包含所有名称。在 for 循环中,这将是 row['text']。
【问题讨论】:
-
请发布您如何创建注释的代码。
标签: pandas matplotlib legend geopandas