如何使用Lambert共形投影显示Cartopy网格线标签?

2024-06-01 12:48:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Cartopy版本0.18在Lambert共形投影中绘制地图。 在下面的代码中,我定义投影并绘制轴。然后添加网格线和网格标签。由于Lambert共形投影,默认情况下在地图上显示经度标签,这是我不希望看到的,因此使用x_inline=False, y_inline=False参数使标签显示在底部。然后,我想指定我希望网格线和标签显示的x和y记号位置。你可以看到我指定的 gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50]) and gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])但是在随机创建的地图上,它没有显示一些标签。为了在指定的位置显示标签,是否有我没有考虑的事情,或者需要传递一个参数?我知道使用Lambert共形投影绘制栅格标签是Cartopy中的一个新功能,所以我不确定这是否只是一个警告,或者是否存在修复

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import ticker
import proplot

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN.with_scale('10m'),facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale('10m'),facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale('10m'),edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])
plt.show()

Map Example Output


Tags: importfalseasinline标签axfeature投影
2条回答

网格线附带的标签不是随机创建/打印的。如果可以在不碰撞现有对象的情况下打印每个单独的对象,则会创建并检查这些对象。如果提供了足够的空间,且未发现冲突,则将打印所有标签。 有时,将figsize设置为更大的尺寸可以解决此问题

import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib import ticker
#import proplot

plt.figure(figsize=[12,12])

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
res = '110m' #'50m', '110m'
ax.add_feature(cfeature.OCEAN.with_scale(res),facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale(res),facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES.with_scale(res),edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])
plt.show()

plot1

我使用了你的代码,去掉了10米的分辨率和import proplot,得到了你想要的

import matplotlib.pyplot as plt
from matplotlib import ticker

proj = ccrs.LambertConformal(central_longitude=-87.5, central_latitude=32.5)
ax = plt.axes(projection=proj)
ax.set_extent([-100,-70,24,50], ccrs.PlateCarree())
ax.add_feature(cfeature.OCEAN,facecolor='paleturquoise',alpha=0.4)
ax.add_feature(cfeature.STATES,facecolor='olivedrab',alpha=0.4)
ax.add_feature(cfeature.STATES,edgecolor='black')
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, x_inline=False, y_inline=False, linewidth=0.33, color='k',alpha=0.5)
gl.right_labels = gl.top_labels = False
gl.ylocator = ticker.FixedLocator([25,30,35,40, 45, 50])
gl.xlocator = ticker.FixedLocator([-100, -95, -90, -85, -80, -75, -70])

enter image description here

相关问题 更多 >