简体   繁体   中英

Rotate latitude (y) tick labels - cartopy

Is there a way to rotate the latitude tick labels in a cartopy map? 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.

Below there's a simple example (I plot my own shapefiles, that I don't express here):

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. My question is: is there a way to rotate the latitude tick labels (or the y tick labels) in 90° so they get vertical?

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.

For rotating tick labels, there is an example in the matplotlib gallery .

So, to rotate your y tick labels you just need to add the following to your code:

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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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