大家晚上好!
今天我尝试建立一个系统,使用 python (3.5)、matplotlib (2.2.3) 和 Cartopy (0.16) 自动绘制网格数据,所有这些都在 Windows 10 下。我想获得 xticks 和 yticks 值(图中所示的坐标)并将它们显示给用户(他可以设置自己的值)。问题是方法“get_xticks()”和“get_yticks()”似乎给出了奇怪的结果。
这是一个示例代码:
import numpy
import matplotlib.pyplot as plt
import cartopy
lon = numpy.arange(-180,180,1)
lat = numpy.arange(-90,90,1)
data = numpy.ones((len(lat), len(lon)))
ax = plt.subplot(1, 1, 1, projection=cartopy.crs.PlateCarree())
ax.pcolormesh(lon, lat, data, transform=cartopy.crs.PlateCarree(), cmap='jet')
ax.set_extent((-180,180,-90,90), cartopy.crs.PlateCarree())
ax.coastlines()
shp_file = cartopy.io.shapereader.natural_earth(resolution='110m', category='physical', name='land')
for land in cartopy.io.shapereader.Reader(shp_file).records():
ax.add_geometries(land.geometry, cartopy.crs.PlateCarree(), facecolor='#e6e6e6')
gl = ax.gridlines(crs=cartopy.crs.PlateCarree(), draw_labels=True,
linewidth=1, color='black', alpha=1, linestyle='-')
gl.xlabels_top = True
gl.ylabels_left = True
gl.xlines = True
gl.ylines = True
plt.show()
print(gl.axes.get_xticks())
print(gl.axes.get_yticks())
前面的结果是:
[-200. -150. -100. -50. 0. 50. 100. 150. 200.] for X axis
[-100. -75. -50. -25. 0. 25. 50. 75. 100.] for Y axis
我无法理解 Cartopy 和 Matplotlib 在哪里采用这些值......而且当我刚开始使用 Cartopy 时,我无法想象它是一个错误......我习惯使用 Matplotlib 来绘制不同类型的的时间序列。但在这里,我正在努力了解正在发生的事情并找到解决方案。
所以我的问题很简单,有没有一种简单的方法可以得到如图所示的xticks和yticks值?
非常感谢您的帮助!
奥利维尔
请您参考如下方法:
我不确定,但我认为线索在 gridliner 默认使用的 matplotlib.ticker.MaxNLocator 对象中:
print(gl.xlocator.tick_values(*ax.get_xlim()))
print(gl.ylocator.tick_values(*ax.get_ylim()))
给予
[-180. -120. -60. 0. 60. 120. 180.]
[-100. -80. -60. -40. -20. 0. 20. 40. 60. 80. 100.]
