@@ -758,6 +758,18 @@ def get_children(self):
758
758
return [self .label , self .offsetText ,
759
759
* self .get_major_ticks (), * self .get_minor_ticks ()]
760
760
761
+ def _reset_major_tick_kw (self ):
762
+ self ._major_tick_kw .clear ()
763
+ self ._major_tick_kw ['gridOn' ] = (
764
+ mpl .rcParams ['axes.grid' ] and
765
+ mpl .rcParams ['axes.grid.which' ] in ('both' , 'major' ))
766
+
767
+ def _reset_minor_tick_kw (self ):
768
+ self ._minor_tick_kw .clear ()
769
+ self ._minor_tick_kw ['gridOn' ] = (
770
+ mpl .rcParams ['axes.grid' ] and
771
+ mpl .rcParams ['axes.grid.which' ] in ('both' , 'minor' ))
772
+
761
773
def clear (self ):
762
774
"""
763
775
Clear the axis.
@@ -779,14 +791,8 @@ def clear(self):
779
791
# Clear the callback registry for this axis, or it may "leak"
780
792
self .callbacks = cbook .CallbackRegistry ()
781
793
782
- # whether the grids are on
783
- self ._major_tick_kw ['gridOn' ] = (
784
- mpl .rcParams ['axes.grid' ] and
785
- mpl .rcParams ['axes.grid.which' ] in ('both' , 'major' ))
786
- self ._minor_tick_kw ['gridOn' ] = (
787
- mpl .rcParams ['axes.grid' ] and
788
- mpl .rcParams ['axes.grid.which' ] in ('both' , 'minor' ))
789
-
794
+ self ._reset_major_tick_kw ()
795
+ self ._reset_minor_tick_kw ()
790
796
self .reset_ticks ()
791
797
792
798
self .converter = None
@@ -833,10 +839,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
833
839
# future new ticks will automatically get them
834
840
if reset :
835
841
if which in ['major' , 'both' ]:
836
- self ._major_tick_kw . clear ()
842
+ self ._reset_major_tick_kw ()
837
843
self ._major_tick_kw .update (kwtrans )
838
844
if which in ['minor' , 'both' ]:
839
- self ._minor_tick_kw . clear ()
845
+ self ._reset_minor_tick_kw ()
840
846
self ._minor_tick_kw .update (kwtrans )
841
847
self .reset_ticks ()
842
848
else :
@@ -1385,35 +1391,40 @@ def grid(self, b=None, which='major', **kwargs):
1385
1391
1386
1392
grid(color='r', linestyle='-', linewidth=2)
1387
1393
"""
1388
- if b is not None :
1389
- if 'visible' in kwargs and bool (b ) != bool (kwargs ['visible' ]):
1394
+ TOGGLE = object ()
1395
+ UNSET = object ()
1396
+ visible = kwargs .pop ('visible' , UNSET )
1397
+
1398
+ if b is None :
1399
+ if visible is UNSET :
1400
+ if kwargs : # grid(color='r')
1401
+ b = True
1402
+ else : # grid()
1403
+ b = TOGGLE
1404
+ else : # grid(visible=v)
1405
+ b = visible
1406
+ else :
1407
+ if visible is not UNSET and bool (b ) != bool (visible ):
1408
+ # grid(True, visible=False), grid(False, visible=True)
1390
1409
raise ValueError (
1391
1410
"'b' and 'visible' specify inconsistent grid visibilities" )
1392
1411
if kwargs and not b : # something false-like but not None
1412
+ # grid(0, visible=True)
1393
1413
_api .warn_external ('First parameter to grid() is false, '
1394
1414
'but line properties are supplied. The '
1395
1415
'grid will be enabled.' )
1396
1416
b = True
1417
+
1397
1418
which = which .lower ()
1398
1419
_api .check_in_list (['major' , 'minor' , 'both' ], which = which )
1399
1420
gridkw = {'grid_' + item [0 ]: item [1 ] for item in kwargs .items ()}
1400
- if 'grid_visible' in gridkw :
1401
- forced_visibility = True
1402
- gridkw ['gridOn' ] = gridkw .pop ('grid_visible' )
1403
- else :
1404
- forced_visibility = False
1405
-
1406
1421
if which in ['minor' , 'both' ]:
1407
- if b is None and not forced_visibility :
1408
- gridkw ['gridOn' ] = not self ._minor_tick_kw ['gridOn' ]
1409
- elif b is not None :
1410
- gridkw ['gridOn' ] = b
1422
+ gridkw ['gridOn' ] = (not self ._minor_tick_kw ['gridOn' ]
1423
+ if b is TOGGLE else b )
1411
1424
self .set_tick_params (which <
23D3
span class=pl-c1>='minor' , ** gridkw )
1412
1425
if which in ['major' , 'both' ]:
1413
- if b is None and not forced_visibility :
1414
- gridkw ['gridOn' ] = not self ._major_tick_kw ['gridOn' ]
1415
- elif b is not None :
1416
- gridkw ['gridOn' ] = b
1426
+ gridkw ['gridOn' ] = (not self ._major_tick_kw ['gridOn' ]
1427
+ if b is TOGGLE else b )
1417
1428
self .set_tick_params (which = 'major' , ** gridkw )
1418
1429
self .stale = True
1419
1430
0 commit comments