如何在Geopandas Geoplot中添加标签?

发布于 2025-01-17 18:04:34 字数 247 浏览 2 评论 0原文

假设我有以下数据集:

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot()

我要实现的目标是用标签绘制数据,以添加列的相应值iso_a3pop_est作为标签图上的每个几何形状。

谢谢

Let's say I have the following dataset:

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot()

What I want to achieve is to plot the data with labels, for instance, to add corresponding values of columns iso_a3 and pop_est as labels on each geometry on the plot.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向日葵 2025-01-24 18:04:34

使用 lambda 函数绘制标签。

fig, ax = plt.subplots(figsize=(20, 10))
world.plot(ax=ax)
world.apply(lambda x: ax.annotate(text=x['iso_a3'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1)

在这种情况下,标签绘制在每个多边形的中心。如果您想绘制更多标签,您可以使用质心加上一个小的偏移量。

Use a lambda function to plot the labels.

fig, ax = plt.subplots(figsize=(20, 10))
world.plot(ax=ax)
world.apply(lambda x: ax.annotate(text=x['iso_a3'], xy=x.geometry.centroid.coords[0], ha='center'), axis=1)

In this case the labels are plotted in the center of each polygon. In case you want to plot more labels you case use the centroid plus a small offset.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文