如何防止 Matplotlib 地图中注释中的标签重叠?

发布于 2025-01-19 18:40:34 字数 1158 浏览 1 评论 0原文

我正在尝试防止下面美国东北部地图中的标签重叠。我试图为该地区的某些州打开和关闭标签,但是肯定有一种更好的方法。以下是我的代码和输出。

csv = pd.read_csv(r'C:\Downloads\Data.csv')
sf = r'C:\Downloads\s_11au16\s_11au16.shp'
US = gpd.read_file(sf)

#Merge them
data = gpd.GeoDataFrame(csv.merge(US))

#set projection
data = data.to_crs(epsg=6923)

#set up basemap
ax = data.plot(figsize = (12,8), column="soil_data", cmap="Greens", edgecolor='black', linewidth=.5, vmin=0, vmax=70, 
               missing_kwds={"color": "white", "edgecolor": "k", "label": "none"})
ax.set_title("Example", fontsize=18, fontweight='bold')
ax.set_axis_off()


#annotate data
label = data.dropna(subset='soil_data')
label.apply(lambda x: ax.annotate(text=int(x['soil_data']), xy=x.geometry.centroid.coords[0], color="black", 
                                  ha='center', fontsize=14, path_effects=[pe.withStroke(linewidth=3, 
                                                                                        foreground="white")]), axis=1)

I am trying to prevent the labels in the Northeast US map below from overlapping. I have tried to turn labels on and off for certain states in the region, but there definitely is a better way of doing it. Below is my code and output.

csv = pd.read_csv(r'C:\Downloads\Data.csv')
sf = r'C:\Downloads\s_11au16\s_11au16.shp'
US = gpd.read_file(sf)

#Merge them
data = gpd.GeoDataFrame(csv.merge(US))

#set projection
data = data.to_crs(epsg=6923)

#set up basemap
ax = data.plot(figsize = (12,8), column="soil_data", cmap="Greens", edgecolor='black', linewidth=.5, vmin=0, vmax=70, 
               missing_kwds={"color": "white", "edgecolor": "k", "label": "none"})
ax.set_title("Example", fontsize=18, fontweight='bold')
ax.set_axis_off()


#annotate data
label = data.dropna(subset='soil_data')
label.apply(lambda x: ax.annotate(text=int(x['soil_data']), xy=x.geometry.centroid.coords[0], color="black", 
                                  ha='center', fontsize=14, path_effects=[pe.withStroke(linewidth=3, 
                                                                                        foreground="white")]), axis=1)

Output map with overlapping labels in the Northeast US

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

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

发布评论

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

评论(1

屋檐 2025-01-26 18:40:34

显然,如果没有您的数据,我无法对其进行测试,但如果您愿意使用 adjustText 再次尝试,您可以尝试将您的 label.apply(...) 替换为类似的内容:

texts = []
for i, row in label.iterrows(): 
  texts.append(ax.annotate(text=int(row['soil_data']), xy=row.geometry.centroid.coords[0], color="black", 
                                  ha='center', fontsize=14, path_effects=[pe.withStroke(linewidth=3, 
                                                                                        foreground="white")]))

adjust_text(texts)

我不知道 adjust_text 如何处理注释,所以如果这不起作用,您可以尝试将其转换为 plt.text

(matplotlib 类 注释继承自 Text 类)

Obviously I cannot test it without your data but if you're willing to try again with adjustText you could try replacing your label.apply(...) with something like that:

texts = []
for i, row in label.iterrows(): 
  texts.append(ax.annotate(text=int(row['soil_data']), xy=row.geometry.centroid.coords[0], color="black", 
                                  ha='center', fontsize=14, path_effects=[pe.withStroke(linewidth=3, 
                                                                                        foreground="white")]))

adjust_text(texts)

I don't know how adjust_text deals with annotations, so if this doesn't work, you could try converting it to plt.text.

(The matplotlib class Annotation inherits from the Text class)

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