2424
2525from .. import constructor
2626from .. import proj as pproj
27+ from .. import ticker as pticker
2728from ..config import rc
28- from ..internals import ic # noqa: F401
2929from ..internals import (
3030 _not_none ,
3131 _pop_rc ,
3232 _version_cartopy ,
3333 docstring ,
34+ ic , # noqa: F401
3435 warnings ,
3536)
36- from .. import ticker as pticker
3737from ..utils import units
38- from . import plot
39- from . import shared
38+ from . import plot , shared
4039
4140try :
4241 import cartopy .crs as ccrs
148147 *For cartopy axes only.*
149148 Whether to rotate non-inline gridline labels so that they automatically
150149 follow the map boundary curvature.
150+ labelrotation : float, optional
151+ The rotation angle in degrees for both longitude and latitude tick labels.
152+ Use `lonlabelrotation` and `latlabelrotation` to set them separately.
153+ lonlabelrotation : float, optional
154+ The rotation angle in degrees for longitude tick labels.
155+ Works for both cartopy and basemap backends.
156+ latlabelrotation : float, optional
157+ The rotation angle in degrees for latitude tick labels.
158+ Works for both cartopy and basemap backends.
151159labelpad : unit-spec, default: :rc:`grid.labelpad`
152160 *For cartopy axes only.*
153161 The padding between non-inline gridline labels and the map boundary.
@@ -850,6 +858,9 @@ def format(
850858 latlabels = None ,
851859 lonlabels = None ,
852860 rotatelabels = None ,
861+ labelrotation = None ,
862+ lonlabelrotation = None ,
863+ latlabelrotation = None ,
853864 loninline = None ,
854865 latinline = None ,
855866 inlinelabels = None ,
@@ -996,6 +1007,8 @@ def format(
9961007 rotatelabels = _not_none (
9971008 rotatelabels , rc .find ("grid.rotatelabels" , context = True )
9981009 ) # noqa: E501
1010+ lonlabelrotation = _not_none (lonlabelrotation , labelrotation )
1011+ latlabelrotation = _not_none (latlabelrotation , labelrotation )
9991012 labelpad = _not_none (labelpad , rc .find ("grid.labelpad" , context = True ))
10001013 dms = _not_none (dms , rc .find ("grid.dmslabels" , context = True ))
10011014 nsteps = _not_none (nsteps , rc .find ("grid.nsteps" , context = True ))
@@ -1028,6 +1041,8 @@ def format(
10281041 loninline = loninline ,
10291042 latinline = latinline ,
10301043 rotatelabels = rotatelabels ,
1044+ lonlabelrotation = lonlabelrotation ,
1045+ latlabelrotation = latlabelrotation ,
10311046 labelpad = labelpad ,
10321047 nsteps = nsteps ,
10331048 )
@@ -1690,6 +1705,8 @@ def _update_major_gridlines(
16901705 latinline = None ,
16911706 labelpad = None ,
16921707 rotatelabels = None ,
1708+ lonlabelrotation = None ,
1709+ latlabelrotation = None ,
16931710 nsteps = None ,
16941711 ):
16951712 """
@@ -1729,6 +1746,10 @@ def _update_major_gridlines(
17291746 gl .y_inline = bool (latinline )
17301747 if rotatelabels is not None :
17311748 gl .rotate_labels = bool (rotatelabels ) # ignored in cartopy < 0.18
1749+ if lonlabelrotation is not None :
1750+ gl .xlabel_style ["rotation" ] = lonlabelrotation
1751+ if latlabelrotation is not None :
1752+ gl .ylabel_style ["rotation" ] = latlabelrotation
17321753 if latinline is not None or loninline is not None :
17331754 lon , lat = loninline , latinline
17341755 b = True if lon and lat else "x" if lon else "y" if lat else None
@@ -2108,17 +2129,20 @@ def _update_gridlines(
21082129 latgrid = None ,
21092130 lonarray = None ,
21102131 latarray = None ,
2132+ lonlabelrotation = None ,
2133+ latlabelrotation = None ,
21112134 ):
21122135 """
21132136 Apply changes to the basemap axes.
21142137 """
21152138 latmax = self ._lataxis .get_latmax ()
2116- for axis , name , grid , array , method in zip (
2139+ for axis , name , grid , array , method , rotation in zip (
21172140 ("x" , "y" ),
21182141 ("lon" , "lat" ),
21192142 (longrid , latgrid ),
21202143 (lonarray , latarray ),
21212144 ("drawmeridians" , "drawparallels" ),
2145+ (lonlabelrotation , latlabelrotation ),
21222146 ):
21232147 # Correct lonarray and latarray label toggles by changing from lrbt to lrtb.
21242148 # Then update the cahced toggle array. This lets us change gridline locs
@@ -2173,6 +2197,9 @@ def _update_gridlines(
21732197 for obj in self ._iter_gridlines (objs ):
21742198 if isinstance (obj , mtext .Text ):
21752199 obj .update (kwtext )
2200+ # Apply rotation if specified
2201+ if rotation is not None :
2202+ obj .set_rotation (rotation )
21762203 else :
21772204 obj .update (kwlines )
21782205
@@ -2191,6 +2218,8 @@ def _update_major_gridlines(
21912218 loninline = None ,
21922219 latinline = None ,
21932220 rotatelabels = None ,
2221+ lonlabelrotation = None ,
2222+ latlabelrotation = None ,
21942223 labelpad = None ,
21952224 nsteps = None ,
21962225 ):
@@ -2204,6 +2233,8 @@ def _update_major_gridlines(
22042233 latgrid = latgrid ,
22052234 lonarray = lonarray ,
22062235 latarray = latarray ,
2236+ lonlabelrotation = lonlabelrotation ,
2237+ latlabelrotation = latlabelrotation ,
22072238 )
22082239 sides = {}
22092240 for side , lonon , laton in zip (
@@ -2226,6 +2257,8 @@ def _update_minor_gridlines(self, longrid=None, latgrid=None, nsteps=None):
22262257 latgrid = latgrid ,
22272258 lonarray = array ,
22282259 latarray = array ,
2260+ lonlabelrotation = None ,
2261+ latlabelrotation = None ,
22292262 )
22302263 # Set isDefault_majloc, etc. to True for both axes
22312264 # NOTE: This cannot be done inside _update_gridlines or minor gridlines
0 commit comments