@@ -786,6 +786,18 @@ def get_children(self):
786
786
return [self .label , self .offsetText ,
787
787
* self .get_major_ticks (), * self .get_minor_ticks ()]
788
788
789
+ def _reset_major_tick_kw (self ):
790
+ self ._major_tick_kw .clear ()
791
+ self ._major_tick_kw ['gridOn' ] = (
792
+ mpl .rcParams ['axes.grid' ] and
793
+ mpl .rcParams ['axes.grid.which' ] in ('both' , 'major' ))
794
+
795
+ def _reset_minor_tick_kw (self ):
796
+ self ._minor_tick_kw .clear ()
797
+ self ._minor_tick_kw ['gridOn' ] = (
798
+ mpl .rcParams ['axes.grid' ] and
799
+ mpl .rcParams ['axes.grid.which' ] in ('both' , 'minor' ))
800
+
789
801
def clear (self ):
790
802
"""
791
803
Clear the axis.
@@ -807,14 +819,8 @@ def clear(self):
807
819
# Clear the callback registry for this axis, or it may "leak"
808
820
self .callbacks = cbook .CallbackRegistry ()
809
821
810
- # whether the grids are on
811
- self ._major_tick_kw ['gridOn' ] = (
812
- mpl .rcParams ['axes.grid' ] and
813
- mpl .rcParams ['axes.grid.which' ] in ('both' , 'major' ))
814
- self ._minor_tick_kw ['gridOn' ] = (
815
- mpl .rcParams ['axes.grid' ] and
816
- mpl .rcParams ['axes.grid.which' ] in ('both' , 'minor' ))
817
-
822
+ self ._reset_major_tick_kw ()
823
+ self ._reset_minor_tick_kw ()
818
824
self .reset_ticks ()
819
825
820
826
self .converter = None
@@ -861,10 +867,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
861
867
# future new ticks will automatically get them
862
868
if reset :
863
869
if which in ['major' , 'both' ]:
864
- self ._major_tick_kw . clear ()
870
+ self ._reset_major_tick_kw ()
865
871
self ._major_tick_kw .update (kwtrans )
866
872
if which in ['minor' , 'both' ]:
867
- self ._minor_tick_kw . clear ()
873
+ self ._reset_minor_tick_kw ()
868
874
self ._minor_tick_kw .update (kwtrans )
869
875
self .reset_ticks ()
870
876
else :
@@ -1413,35 +1419,40 @@ def grid(self, b=None, which='major', **kwargs):
1413
1419
1414
1420
grid(color='r', linestyle='-', linewidth=2)
1415
1421
"""
1416
- if b is not None :
1417
- if 'visible' in kwargs and bool (b ) != bool (kwargs ['visible' ]):
1422
+ TOGGLE = object ()
1423
+ UNSET = object ()
1424
+ visible = kwargs .pop ('visible' , UNSET )
1425
+
1426
+ if b is None :
1427
+ if visible is UNSET :
1428
+ if kwargs : # grid(color='r')
1429
+ b = True
1430
+ else : # grid()
1431
+ b = TOGGLE
1432
+ else : # grid(visible=v)
1433
+ b = visible
1434
+ else :
1435
+ if visible is not UNSET and bool (b ) != bool (visible ):
1436
+ # grid(True, visible=False), grid(False, visible=True)
1418
1437
raise ValueError (
1419
1438
"'b' and 'visible' specify inconsistent grid visibilities" )
1420
1439
if kwargs and not b : # something false-like but not None
1440
+ # grid(0, visible=True)
1421
1441
_api .warn_external ('First parameter to grid() is false, '
1422
1442
'but line properties are supplied. The '
1423
1443
'grid will be enabled.' )
1424
1444
b = True
1445
+
1425
1446
which = which .lower ()
1426
1447
_api .check_in_list (['major' , 'minor' , 'both' ], which = which )
1427
1448
gridkw = {'grid_' + item [0 ]: item [1 ] for item in kwargs .items ()}
1428
- if 'grid_visible' in gridkw :
1429
- forced_visibility = True
1430
- gridkw ['gridOn' ] = gridkw .pop ('grid_visible' )
1431
- else :
1432
- forced_visibility = False
1433
-
1434
1449
if which in ['minor' , 'both' ]:
1435
- if b is None and not forced_visibility :
1436
- gridkw ['gridOn' ] = not self ._minor_tick_kw ['gridOn' ]
1437
- elif b is not None :
1438
- gridkw ['gridOn' ] = b
1450
+ gridkw ['gridOn' ] = (not self ._minor_tick_kw ['gridOn' ]
1451
+ if b is TOGGLE else b )
1439
1452
self .set_tick_params (which = 'minor' , ** gridkw )
1440
1453
if which in ['major' , 'both' ]:
1441
- if b is None and not forced_visibility :
1442
- gridkw ['gridOn' ] = not self ._major_tick_kw ['gridOn' ]
1443
- elif b is not None :
1444
- gridkw ['gridOn' ] = b
1454
+ gridkw ['gridOn' ] = (not self ._major_tick_kw ['gridOn' ]
1455
+ if b is TOGGLE else b )
1445
1456
self .set_tick_params (which = 'major' , ** gridkw )
1446
1457
self .stale = True
1447
1458
0 commit comments