首页 > 解决方案 > 声明后如何关闭cartopy中的网格线?

问题描述

我想添加一个选项来使用函数打开/关闭网格线。但是一旦打开网格线就无法禁用它,我尝试将 xlines 和 ylines 设置为 False 但没有按预期的方式工作,有没有其他选择?我正在使用 matplotlib.figure.figure 而不是 plt。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.gridlines()

有没有办法从轴上删除网格线?

标签: matplotlibcartopy

解决方案


要从轴上的绘图中删除网格线,您可以执行以下操作:

import matplotlib.pyplot as plt
import cartopy.crs as ccrs

ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()

# .gridlines() creates `gridliner` object
# grab it and assign it as `gl`
gl = ax.gridlines()

# manipulate `gridliner` object
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.ylines = False

plt.show()

推荐阅读