简体   繁体   English

旋转纬度 (y) 刻度标签 - cartopy

[英]Rotate latitude (y) tick labels - cartopy

Is there a way to rotate the latitude tick labels in a cartopy map?有没有办法在 cartopy 地图中旋转纬度刻度标签? I know it is a very easy and usual option in GIS programs such as QGIS, but I couldn't find anything on the subject when using cartopy.我知道在 QGIS 等 GIS 程序中这是一个非常简单且常用的选项,但是在使用 cartopy 时我找不到有关该主题的任何内容。

Below there's a simple example (I plot my own shapefiles, that I don't express here):下面是一个简单的例子(我绘制了我自己的 shapefile,我没有在这里表达):

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter

mapp = plt.subplot(111, projection=ccrs.PlateCarree())
mapp.set_extent([-44.2, -45.6, -23.36, -24.35])

mapp.set_yticks([-23.5,-24], crs=ccrs.PlateCarree())
mapp.set_xticks([-44.5,-45.5], crs=ccrs.PlateCarree())

lon_formatter = LongitudeFormatter(number_format='.1f')
lat_formatter = LatitudeFormatter(number_format='.1f')
mapp.xaxis.set_major_formatter(lon_formatter)
mapp.yaxis.set_major_formatter(lat_formatter)
plt.show()

It's not the best MCVE, but anyway, it shows the latitude tick labels horizontally -- that's a standard.它不是最好的 MCVE,但无论如何,它会水平显示纬度刻度标签——这是一个标准。 My question is: is there a way to rotate the latitude tick labels (or the y tick labels) in 90° so they get vertical?我的问题是:有没有办法将纬度刻度标签(或 y 刻度标签)旋转 90°,使它们垂直?

As a cartopy axes is just a special matplotlib axes, a lot of what you can do to a matplotlib plot, you can also do to a cartopy plot.由于 cartopy 轴只是一个特殊的 matplotlib 轴,因此您可以对 matplotlib 图执行很多操作,也可以对 cartopy 图执行很多操作。

For rotating tick labels, there is an example in the matplotlib gallery .对于旋转刻度标签, matplotlib 库中有一个示例

So, to rotate your y tick labels you just need to add the following to your code:因此,要旋转您的 y 刻度标签,您只需将以下内容添加到您的代码中:

plt.yticks(rotation='vertical')

I also noticed that your map extents are in the wrong order.我还注意到您的地图范围的顺序错误。 The order should be顺序应该是

[x_min, x_max, y_min, y_max]

So your code should look like:所以你的代码应该是这样的:

mapp.set_extent([-24.35, -23.36, -45.6, -44.2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM