地图映射注释不可见
我不明白为什么我不能注释我的美国地图。
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.
Thank you in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保使用您通过各种图和注释一致创建的图形和轴对象。看来您正在使用consition_gdf.plot(忽略无花果和斧头)创建一个新图,但是然后使用原始
ax
进行注释。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.