@@ -758,6 +758,18 @@ def get_children(self):
758758 return [self .label , self .offsetText ,
759759 * self .get_major_ticks (), * self .get_minor_ticks ()]
760760
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+
761773 def clear (self ):
762774 """
763775 Clear the axis.
@@ -779,14 +791,8 @@ def clear(self):
779791 # Clear the callback registry for this axis, or it may "leak"
780792 self .callbacks = cbook .CallbackRegistry ()
781793
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 ()
790796 self .reset_ticks ()
791797
792798 self .converter = None
@@ -833,10 +839,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
833839 # future new ticks will automatically get them
834840 if reset :
835841 if which in ['major' , 'both' ]:
836- self ._major_tick_kw . clear ()
842+ self ._reset_major_tick_kw ()
837843 self ._major_tick_kw .update (kwtrans )
838844 if which in ['minor' , 'both' ]:
839- self ._minor_tick_kw . clear ()
845+ self ._reset_minor_tick_kw ()
840846 self ._minor_tick_kw .update (kwtrans )
841847 self .reset_ticks ()
842848 else :
@@ -1385,35 +1391,40 @@ def grid(self, b=None, which='major', **kwargs):
13851391
13861392 grid(color='r', linestyle='-', linewidth=2)
13871393 """
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)
13901409 raise ValueError (
13911410 "'b' and 'visible' specify inconsistent grid visibilities" )
13921411 if kwargs and not b : # something false-like but not None
1412+ # grid(0, visible=True)
13931413 _api .warn_external ('First parameter to grid() is false, '
13941414 'but line properties are supplied. The '
13951415 'grid will be enabled.' )
13961416 b = True
1417+
13971418 which = which .lower ()
13981419 _api .check_in_list (['major' , 'minor' , 'both' ], which = which )
13991420 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-
14061421 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 )
14111424 self .set_tick_params (which = 'minor' , ** gridkw )
14121425 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 )
14171428 self .set_tick_params (which = 'major' , ** gridkw )
14181429 self .stale = True
14191430
0 commit comments