python - Cartopy:显示轴的刻度线
问题描述
我想在外面显示轴刻度线。Cartopy 删除轴刻度线!我在这里尝试了解决方案。
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter,
LatitudeLocator, LongitudeLocator)
fig, ax = plt.subplots(figsize=(10, 5), subplot_kw={"projection":ccrs.PlateCarree()})
ax.coastlines()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=0.5, linestyle='--')
gl.top_labels = False
gl.left_labels = False
gl.xlocator = LongitudeLocator()
gl.ylocator = LatitudeLocator()
gl.xformatter = LongitudeFormatter(auto_hide=False)
gl.yformatter = LatitudeFormatter()
ax.tick_params(axis="both",
tickdir='out',
length=15,
grid_transform=ccrs.PlateCarree()) # this did not work
gl.axes.tick_params(axis="both",
tickdir='out',
length=15,
grid_transform=ccrs.PlateCarree()) # this also did not work
plt.show()
解决方案
您可以使用带有相关 crs 的 matplotlib set_xticks 为绘图添加刻度线。然后分别添加网格线
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import (LongitudeFormatter, LatitudeFormatter,
LatitudeLocator, LongitudeLocator)
fig, ax = plt.subplots(figsize=(10, 5), subplot_kw={"projection":ccrs.PlateCarree()})
ax.coastlines()
ax.yaxis.tick_right()
ax.set_xticks([-180,-120, -60, 0, 60, 120, 180], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True)
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False,
linewidth=2, color='gray', alpha=0.5, linestyle='--')
基于https://scitools.org.uk/cartopy/docs/v0.16/gallery/tick_labels.html
推荐阅读
- javascript - 如何为 React Carousel 组件中的图像元素暂停 setInterval?
- selenium - 执行卡在 selenium webdriver 的日期选择功能上
- python - Python 函数:避免使用 if 子句进行参数检查
- database - 存在特定字段的文档的 MongoDb 计数百分比
- angular - Camunda 使用 REST 获得 XOR 网关的机会
- python - 如何删除错误消息:没有找到这样的文件或目录 Python?
- json - 删除 XSLT 3.0 xml-to-json 转换结果中的引号
- .net - 使用异步 .NET 方法作为 Win32 API 回调
- java - Excel 文件保存在 Eclipse jee 文件夹中,而不是项目文件夹中
- angular - 无法以 Angular 7 下载 zip 文件