地图映射注释不可见

发布于 2025-02-05 11:55:26 字数 1036 浏览 4 评论 0原文

我不明白为什么我不能注释我的美国地图。

import fiona
import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon,box


countries_gdf = geopandas.read_file("geopkg/USA.gpkg",layer="ADM_1")
polygon = box(-130, -70, 0, 50)
countries_gdf = geopandas.clip(countries_gdf, polygon)

f = plt.figure()
ax = f.add_subplot()

all_states = countries_gdf.plot(facecolor="gray",edgecolor="black",linewidth=.15,rasterized=True)

tx = countries_gdf.query('HASC_1 == "US.TX" or HASC_1 == "US.MI"')
tx.plot(ax=all_states,rasterized=True)
tx.apply(lambda x: ax.annotate(text=x.NAME_1, xy=x.geometry.centroid.coords[0], ha='center'),axis=1)

plt.savefig("test3.png",dpi=500)

该代码不会崩溃,也不会显示任何内容。

我用作GPKG文件,该网站上存在的文件: https://gadm.org/gadm.org/download_country.htmltry.html 。

问题可能是CRS投影系统吗? 我还试图手动输入坐标,但也没有结果。

“无注释的地图”

预先感谢您,

I don't understand why I can't annotate my map of the United States.

import fiona
import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon,box


countries_gdf = geopandas.read_file("geopkg/USA.gpkg",layer="ADM_1")
polygon = box(-130, -70, 0, 50)
countries_gdf = geopandas.clip(countries_gdf, polygon)

f = plt.figure()
ax = f.add_subplot()

all_states = countries_gdf.plot(facecolor="gray",edgecolor="black",linewidth=.15,rasterized=True)

tx = countries_gdf.query('HASC_1 == "US.TX" or HASC_1 == "US.MI"')
tx.plot(ax=all_states,rasterized=True)
tx.apply(lambda x: ax.annotate(text=x.NAME_1, xy=x.geometry.centroid.coords[0], ha='center'),axis=1)

plt.savefig("test3.png",dpi=500)

The code does not crash and does not display anything.

I use as gpkg file, the one present on this site: https://gadm.org/download_country.html.

Could the problem be the CRS projection system?
I also tried to enter the coordinates manually but without results either.

Map without annotation

Thank you in advance,

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

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

发布评论

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

评论(1

单挑你×的.吻 2025-02-12 11:55:27

确保使用您通过各种图和注释一致创建的图形和轴对象。看来您正在使用consition_gdf.plot(忽略无花果和斧头)创建一个新图,但是然后使用原始ax进行注释。

import fiona
import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon,box

countries_gdf = geopandas.read_file("geopkg/USA.gpkg",layer="ADM_1")
polygon = box(-130, -70, 0, 50)
countries_gdf = geopandas.clip(countries_gdf, polygon)

f = plt.figure()
ax = f.add_subplot()

# make sure you plot all states using ax
countries_gdf.plot(
    facecolor="gray", edgecolor="black", linewidth=.15, rasterized=True,
    # add this
    ax=ax,
)

tx = countries_gdf.query('HASC_1 == "US.TX" or HASC_1 == "US.MI"')
tx.plot(ax=ax, rasterized=True)
tx.apply(
    lambda x: ax.annotate(
        text=x.NAME_1,
        xy=x.geometry.centroid.coords[0],
        ha='center',
    ),
    axis=1,
)

fig.savefig("test3.png",dpi=500)

Make sure to use the figure and axis objects you create consistently through the various plots and annotations. It looks like you were creating a new plot with countries_gdf.plot (ignoring fig and ax), but then using the original ax to annotate.

import fiona
import geopandas
import matplotlib.pyplot as plt
from shapely.geometry import Polygon,box

countries_gdf = geopandas.read_file("geopkg/USA.gpkg",layer="ADM_1")
polygon = box(-130, -70, 0, 50)
countries_gdf = geopandas.clip(countries_gdf, polygon)

f = plt.figure()
ax = f.add_subplot()

# make sure you plot all states using ax
countries_gdf.plot(
    facecolor="gray", edgecolor="black", linewidth=.15, rasterized=True,
    # add this
    ax=ax,
)

tx = countries_gdf.query('HASC_1 == "US.TX" or HASC_1 == "US.MI"')
tx.plot(ax=ax, rasterized=True)
tx.apply(
    lambda x: ax.annotate(
        text=x.NAME_1,
        xy=x.geometry.centroid.coords[0],
        ha='center',
    ),
    axis=1,
)

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