Cartopy:显示轴的记号

2024-06-17 09:06:41 发布

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

我想在外面显示轴记号。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()

enter image description here


Tags: importfalselabelsaspltaxglcartopy
1条回答
网友
1楼 · 发布于 2024-06-17 09:06:41

可以使用带有相关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

cartopy global PlateCarree map with tick marks and grid lines

相关问题 更多 >