jus*_*ile 2 matplotlib python-3.x axis-labels cartopy
right_labels = False
我试图使用但仅适用于某些值来隐藏此 Cartopy 地图中的右侧标签(又名纬度) 。top_labels = False
但是,可以使用和隐藏顶部/底部标签bottom_label=False
。
重现:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.ticker as mticker
import matplotlib.path as mpath
import cartopy.feature as cf
"""
Plot Alaska
"""
# Map View Using Cartopy
fig = plt.figure(figsize=(8,6))
xmin=-163
xmax=-120
ymin=50
ymax=71
proj = ccrs.LambertConformal(central_longitude=(xmin+xmax)/2, central_latitude=(ymin+ymax)/2)
ax = fig.add_subplot(1, 1, 1, projection=proj)
n = 20
aoi = mpath.Path(
list(zip(np.linspace(xmin, xmax, n), np.full(n, ymax))) + \
list(zip(np.full(n, xmax), np.linspace(ymax, ymin, n))) + \
list(zip(np.linspace(xmax, xmin, n), np.full(n, ymin))) + \
list(zip(np.full(n, xmin), np.linspace(ymin, ymax, n)))
)
ax.set_boundary(aoi, transform=ccrs.PlateCarree())
# Plot Ocean Borders
ocean = cf.NaturalEarthFeature('physical', 'ocean', scale='50m', edgecolor='k', facecolor='lightblue', lw=1,
linestyle='-')
ax.add_feature(ocean)
# Colored Land Background
land = cf.NaturalEarthFeature('physical', 'land', scale='50m', facecolor='snow', lw=1, linestyle='--')
ax.add_feature(land)
ax.set_extent([xmin, xmax, ymin, ymax], crs=ccrs.PlateCarree())
# Set gridlines to variable so you can manipulate them
gl = ax.gridlines(draw_labels=True, crs=ccrs.PlateCarree(), x_inline=False, y_inline=False)
gl.xlocator = mticker.FixedLocator([-160, -150, -140, -130, -120])
gl.ylocator = mticker.FixedLocator([50, 55, 60, 65, 70])
gl.top_labels = False
gl.right_labels = False
plt.show()
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
我怀疑这与使用ax.set_boundary()
制作地图有关,但在网上查看后,我找不到任何东西,除了这个GitHub 问题提到了这个问题,但它已关闭,所以我想他们已经修复了它。我正在使用 Cartopy 版本 0.21.1。
这个问题是在研究另一个 SO 问题时产生的:Cartopy 标签没有出现在高纬度非矩形投影中(所有代码均归功于@kbiegseismic)。
很明显,这ax.set_boundary()
混淆了内部的过程.gridlines()
,特别是当它需要在这种特殊情况下隐藏一些 y 标签时。因此,某些标签的可见性设置错误。
我们需要等待 Cartopy 的下一个版本才能看到更好的结果gridlines()
功能。如果您需要解决方法,可以尝试下面的代码片段。只需将其放在现有代码的末尾即可。
# Generate the plot to enable access to the labels\' attributes\nplt.draw()\n\n# Iterate for the y-labels\n# The right labels have x coordinates > 0\n# The left labels < 0\nfor ea in gl.ylabel_artists:\n right_label = ea.get_position()[0] > 0\n # print(ea, ea.get_position()[0], ea.get_visible())\n if right_label:\n ea.set_visible(False)\n\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n\n如果您修改地图边界以具有如下所示的小型额外缓冲区:-
\npg = 0.006\n#xmin=-163 -pg\nxmax=-120 +pg\nymin=50 -pg\n#ymax=71 +pg\n
Run Code Online (Sandbox Code Playgroud)\n最终绘图将包括 120\xc2\xb0W 经度和 50\xc2\xb0N 纬度处缺失的标签。
\n\n 归档时间: |
|
查看次数: |
229 次 |
最近记录: |