@@ -585,9 +585,6 @@ def __share_axis_setup(
585
585
if level > 1 and limits :
586
586
self ._share_limits_with (other , which = which )
587
587
588
- if level >= 1 and labels :
589
- self ._share_labels_with_others ()
590
-
591
588
@override
592
589
def _sharey_setup (self , sharey , * , labels = True , limits = True ):
593
590
"""
@@ -676,9 +673,6 @@ def _apply_axis_sharing(self):
676
673
return
677
674
if self .figure ._get_sharing_level () == 0 :
678
675
return
679
- # Share labels with all levels higher or equal
680
- # to 1.
681
- self ._share_labels_with_others ()
682
676
683
677
def _get_gridliner_labels (
684
678
self ,
@@ -687,33 +681,30 @@ def _get_gridliner_labels(
687
681
left = None ,
688
682
right = None ,
689
683
):
690
- assert NotImplementedError ("Should be implemented by Cartopy or Basemap Axes" )
684
+ raise NotImplementedError ("Should be implemented by Cartopy or Basemap Axes" )
691
685
692
686
def _toggle_gridliner_labels (
693
687
self ,
694
- top = None ,
695
- bottom = None ,
696
- left = None ,
697
- right = None ,
688
+ labeltop = None ,
689
+ labelbottom = None ,
690
+ labelleft = None ,
691
+ labelright = None ,
698
692
geo = None ,
699
693
):
700
694
# For BasemapAxes the gridlines are dicts with key as the coordinate and keys the line and label
701
695
# We override the dict here assuming the labels are mut excl due to the N S E W extra chars
702
- if self .gridlines_major is None :
703
- return
704
696
if any (i is None for i in self .gridlines_major ):
705
697
return
706
698
gridlabels = self ._get_gridliner_labels (
707
- bottom = bottom , top = top , left = left , right = right
699
+ bottom = labelbottom , top = labeltop , left = labelleft , right = labelright
708
700
)
709
- for direction , toggle in zip (
710
- "bottom top left right" .split (),
711
- [bottom , top , left , right ],
712
- ):
713
- if toggle is not None :
714
- for label in gridlabels .get (direction , []):
715
- label .set_visible (toggle )
716
- self .stale = True
701
+ bools = [labelbottom , labeltop , labelleft , labelright ]
702
+ directions = "bottom top left right" .split ()
703
+ for direction , toggle in zip (directions , bools ):
704
+ if toggle is None :
705
+ continue
706
+ for label in gridlabels .get (direction , []):
707
+ label .set_visible (toggle )
717
708
718
709
def _handle_axis_sharing (
719
710
self ,
@@ -733,48 +724,6 @@ def _handle_axis_sharing(
733
724
target_axis .set_view_interval (* source_axis .get_view_interval ())
734
725
target_axis .set_minor_locator (source_axis .get_minor_locator ())
735
726
736
- def _share_labels_with_others (self ):
737
- """
738
- Helpers function to ensure the labels
739
- are shared for rectilinear GeoAxes.
740
- """
741
- # Turn all labels off
742
- # Note: this action performs it for all the axes in
743
- # the figure. We use the stale here to only perform
744
- # it once as it is an expensive action.
745
- border_axes = self .figure ._get_border_axes ()
746
- # Recode:
747
- recoded = {}
748
- for direction , axes in border_axes .items ():
749
- for axi in axes :
750
- recoded [axi ] = recoded .get (axi , []) + [direction ]
751
-
752
- # We turn off the tick labels when the scale and
753
- # ticks are shared (level >= 3)
754
- are_ticks_on = False
755
- default = dict (
756
- left = are_ticks_on ,
757
- right = are_ticks_on ,
758
- top = are_ticks_on ,
759
- bottom = are_ticks_on ,
760
- )
761
- for axi in self .figure .axes :
762
- # If users call colorbar on the figure
763
- # an axis is added which needs to skip the
764
- # sharing that is specific for the GeoAxes.
765
- if not isinstance (axi , GeoAxes ):
766
- continue
767
- gridlabels = self ._get_gridliner_labels (
768
- bottom = True , top = True , left = True , right = True
769
- )
770
- sides = recoded .get (axi , [])
771
- tmp = default .copy ()
772
- for side in sides :
773
- if side in gridlabels and gridlabels [side ]:
774
- tmp [side ] = True
775
- axi ._toggle_gridliner_labels (** tmp )
776
- self .stale = False
777
-
778
727
@override
779
728
def draw (self , renderer = None , * args , ** kwargs ):
780
729
# Perform extra post-processing steps
@@ -1465,29 +1414,46 @@ def _get_side_labels() -> tuple:
1465
1414
top_labels = "xlabels_top"
1466
1415
return (left_labels , right_labels , bottom_labels , top_labels )
1467
1416
1417
+ @override
1418
+ def _is_ticklabel_on (self , side : str ) -> bool :
1419
+ """
1420
+ Helper function to check if tick labels are on for a given side.
1421
+ """
1422
+ # Deal with different cartopy versions
1423
+ left_labels , right_labels , bottom_labels , top_labels = self ._get_side_labels ()
1424
+ if self .gridlines_major is None :
1425
+ return False
1426
+ elif side == "labelleft" :
1427
+ return getattr (self .gridlines_major , left_labels )
1428
+ elif side == "labelright" :
1429
+ return getattr (self .gridlines_major , right_labels )
1430
+ elif side == "labelbottom" :
1431
+ return getattr (self .gridlines_major , bottom_labels )
1432
+ elif side == "labeltop" :
1433
+ return getattr (self .gridlines_major , top_labels )
1434
+ else :
1435
+ raise ValueError (f"Invalid side: { side } " )
1436
+
1437
+ @override
1468
1438
def _toggle_gridliner_labels (
1469
1439
self ,
1470
- left = None ,
1471
- right = None ,
1472
- bottom = None ,
1473
- top = None ,
1440
+ labelleft = None ,
1441
+ labelright = None ,
1442
+ labelbottom = None ,
1443
+ labeltop = None ,
1474
1444
geo = None ,
1475
1445
):
1476
1446
"""
1477
1447
Toggle gridliner labels across different cartopy versions.
1478
1448
"""
1479
- left_labels , right_labels , bottom_labels , top_labels = (
1480
- _CartopyAxes ._get_side_labels ()
1481
- )
1449
+ # Retrieve the property name depending
1450
+ # on cartopy version.
1451
+ side_labels = _CartopyAxes ._get_side_labels ()
1452
+ togglers = (labelleft , labelright , labelbottom , labeltop )
1482
1453
gl = self .gridlines_major
1483
- if left is not None :
1484
- setattr (gl , left_labels , left )
1485
- if right is not None :
1486
- setattr (gl , right_labels , right )
1487
- if bottom is not None :
1488
- setattr (gl , bottom_labels , bottom )
1489
- if top is not None :
1490
- setattr (gl , top_labels , top )
1454
+ for toggle , side in zip (togglers , side_labels ):
1455
+ if getattr (gl , side ) != toggle :
1456
+ setattr (gl , side , toggle )
1491
1457
if geo is not None : # only cartopy 0.20 supported but harmless
1492
1458
setattr (gl , "geo_labels" , geo )
1493
1459
@@ -1779,7 +1745,7 @@ def _update_major_gridlines(
1779
1745
sides = dict ()
1780
1746
# The ordering of these sides are important. The arrays are ordered lrbtg
1781
1747
for side , lon , lat in zip (
1782
- "left right bottom top geo" .split (), lonarray , latarray
1748
+ "labelleft labelright labelbottom labeltop geo" .split (), lonarray , latarray
1783
1749
):
1784
1750
if lon and lat :
1785
1751
sides [side ] = True
@@ -1789,7 +1755,8 @@ def _update_major_gridlines(
1789
1755
sides [side ] = "y"
1790
1756
elif lon is not None or lat is not None :
1791
1757
sides [side ] = False
1792
- self ._toggle_gridliner_labels (** sides )
1758
+ if sides :
1759
+ self ._toggle_gridliner_labels (** sides )
1793
1760
1794
1761
def _update_minor_gridlines (self , longrid = None , latgrid = None , nsteps = None ):
1795
1762
"""
@@ -1976,6 +1943,40 @@ def _turnoff_tick_labels(self, locator: mticker.Formatter):
1976
1943
if isinstance (object , mtext .Text ):
1977
1944
object .set_visible (False )
1978
1945
1946
+ def _get_gridliner_labels (
1947
+ self ,
1948
+ bottom = None ,
1949
+ top = None ,
1950
+ left = None ,
1951
+ right = None ,
1952
+ ):
1953
+ directions = "left right top bottom" .split ()
1954
+ bools = [left , right , top , bottom ]
1955
+ sides = {}
1956
+ for direction , is_on in zip (directions , bools ):
1957
+ if is_on is None :
1958
+ continue
1959
+ gl = self .gridlines_major [0 ]
1960
+ if direction in ["left" , "right" ]:
1961
+ gl = self .gridlines_major [1 ]
1962
+ for loc , (lines , labels ) in gl .items ():
1963
+ for label in labels :
1964
+ position = label .get_position ()
1965
+ match direction :
1966
+ case "top" if position [1 ] > 0 :
1967
+ add = True
1968
+ case "bottom" if position [1 ] < 0 :
1969
+ add = True
1970
+ case "left" if position [0 ] < 0 :
1971
+ add = True
1972
+ case "right" if position [0 ] > 0 :
1973
+ add = True
1974
+ case _:
1975
+ add = False
1976
+ if add :
1977
+ sides .setdefault (direction , []).append (label )
1978
+ return sides
1979
+
1979
1980
def _get_lon0 (self ):
1980
1981
"""
1981
1982
Get the central longitude.
@@ -2196,7 +2197,7 @@ def _update_major_gridlines(
2196
2197
)
2197
2198
sides = {}
2198
2199
for side , lonon , laton in zip (
2199
- "left right top bottom geo" .split (), lonarray , latarray
2200
+ "labelleft labelright labeltop labelbottom geo" .split (), lonarray , latarray
2200
2201
):
2201
2202
if lonon or laton :
2202
2203
sides [side ] = True
@@ -2225,13 +2226,7 @@ def _update_minor_gridlines(self, longrid=None, latgrid=None, nsteps=None):
2225
2226
axis .isDefault_minloc = True
2226
2227
2227
2228
@override
2228
- def _get_gridliner_labels (
2229
- self ,
2230
- bottom = None ,
2231
- top = None ,
2232
- left = None ,
2233
- right = None ,
2234
- ):
2229
+ def _is_ticklabel_on (self , side : str ) -> bool :
2235
2230
# For basemap object, the text is organized
2236
2231
# as a dictionary. The keys are the numerical
2237
2232
# location values, and the values are a list
@@ -2245,45 +2240,47 @@ def _get_gridliner_labels(
2245
2240
def group_labels (
2246
2241
labels : list [mtext .Text ],
2247
2242
which : str ,
2248
- bottom = None ,
2249
- top = None ,
2250
- left = None ,
2251
- right = None ,
2243
+ labelbottom = None ,
2244
+ labeltop = None ,
2245
+ labelleft = None ,
2246
+ labelright = None ,
2252
2247
) -> dict [str , list [mtext .Text ]]:
2253
2248
group = {}
2254
2249
# We take zero here as a baseline
2255
2250
for label in labels :
2256
2251
position = label .get_position ()
2257
2252
target = None
2258
2253
if which == "x" :
2259
- if bottom is not None and position [1 ] < 0 :
2260
- target = "bottom "
2261
- elif top is not None and position [1 ] >= 0 :
2262
- target = "top "
2254
+ if labelbottom is not None and position [1 ] < 0 :
2255
+ target = "labelbottom "
2256
+ elif labeltop is not None and position [1 ] >= 0 :
2257
+ target = "labeltop "
2263
2258
else :
2264
- if left is not None and position [0 ] < 0 :
2265
- target = "left "
2266
- elif right is not None and position [0 ] >= 0 :
2267
- target = "right "
2259
+ if labelleft is not None and position [0 ] < 0 :
2260
+ target = "labelleft "
2261
+ elif labelright is not None and position [0 ] >= 0 :
2262
+ target = "labelright "
2268
2263
if target is not None :
2269
2264
group [target ] = group .get (target , []) + [label ]
2270
2265
return group
2271
2266
2267
+ gl = self .gridlines_major [0 ]
2268
+ which = "x"
2269
+ if side in ["labelleft" , "labelright" ]:
2270
+ gl = self .gridlines_major [1 ]
2271
+ which = "y"
2272
2272
# Group the text object based on their location
2273
2273
grouped = {}
2274
- for which , gl in zip ("xy" , self .gridlines_major ):
2275
- for loc , (line , labels ) in gl .items ():
2276
- tmp = group_labels (
2277
- labels = labels ,
2278
- which = which ,
2279
- bottom = bottom ,
2280
- top = top ,
2281
- left = left ,
2282
- right = right ,
2283
- )
2284
- for key , values in tmp .items ():
2285
- grouped [key ] = grouped .get (key , []) + values
2286
- return grouped
2274
+ for loc , (line , labels ) in gl .items ():
2275
+ labels = group_labels (
2276
+ labels = labels ,
2277
+ which = which ,
2278
+ ** {side : True },
2279
+ )
2280
+ for label in labels .get (side , []):
2281
+ if label .get_visible ():
2282
+ return True
2283
+ return False
2287
2284
2288
2285
2289
2286
# Apply signature obfuscation after storing previous signature
0 commit comments