[英]How to find the right cartopy projection?
I need to plot some data in Germany with cartopy.我需要用 cartopy 在德国绘制一些数据。 The data part of my plot works fine (so I deleted it for now).
我的情节的数据部分工作正常(所以我现在删除了它)。 Unfortunately, the shape of the country is deformed due to the projection.
不幸的是,由于投影,该国的形状发生了变形。
I am currently using the PlateCarree projection, but changing it to Orthographic or others created the same plot.我目前正在使用 PlateCarree 投影,但将其更改为 Orthographic 或其他创建了相同的图。
How to improve the shape?如何改善身材?
Code:代码:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io import shapereader
# get country borders
resolution = '10m'
category = 'cultural'
name = 'admin_0_countries'
shpfilename = shapereader.natural_earth(resolution, category, name)
df = geopandas.read_file(shpfilename)
poly = df.loc[df['ADMIN'] == 'Germany']['geometry'].values[0]
# plot
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([5.8, 15.1, 47.25, 55.1],
crs=ccrs.PlateCarree())
ax.add_geometries(poly,
crs=ccrs.PlateCarree(),
facecolor='gainsboro',
edgecolor='slategray',
lw=0.1,
alpha=.8)
# save plot
save_path = 'germany.png'
plt.savefig(save_path, dpi=250, bbox_inches='tight', pad_inches=0.)
plt.close()
The solution is transforming the Geopandas Dataframe using the same projection as explained here解决方案是使用与此处解释的相同投影来转换 Geopandas 数据框
New output: germany.png新输出: Germany.png
New code:新代码:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import geopandas
from cartopy.io import shapereader
# get country borders
resolution = "10m"
category = "cultural"
name = "admin_0_countries"
shpfilename = shapereader.natural_earth(resolution, category, name)
df = geopandas.read_file(shpfilename)
df_de = df.loc[df["ADMIN"] == "Germany"]
extent = [6., 14.8, 47.1, 55.1]
# plot
crs = ccrs.Orthographic(
central_longitude=(0.5 * (extent[0] + extent[1])),
central_latitude=(0.5 * (extent[2] + extent[3])),
)
crs_proj4 = crs.proj4_init
df_de.crs = "EPSG:4326"
df_ae = df_de.to_crs(crs_proj4)
fig, ax = plt.subplots(subplot_kw={"projection": crs})
ax.set_extent(extent)
ax.add_geometries(
df_ae["geometry"],
crs=crs,
facecolor="gainsboro",
edgecolor="slategray",
lw=0.1,
alpha=0.8,
)
# save plot
save_path = "germany.png"
plt.savefig(save_path, dpi=250, bbox_inches="tight", pad_inches=0.0)
plt.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.