Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3e4a204

Browse files
committed
Fix resetting grid visibility
Actually, there were two issues:- 'gridOn' state should always be contained in the _tick_kw settings. This is needed because we support toggling via grid() and thus need to be able to query the state. Therefore, the _tick_kw dicts must be resetted instead of cleared.- grid(some_kwarg=...) i.e. with b=None and keywords must be treated explicitly as b=True as the docs already propose.Closes#20149.
1 parent363105b commit3e4a204

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

‎lib/matplotlib/axis.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,16 @@ def get_children(self):
786786
return [self.label,self.offsetText,
787787
*self.get_major_ticks(),*self.get_minor_ticks()]
788788

789+
def_reset_major_tick_kw(self):
790+
self._major_tick_kw['gridOn']= (
791+
mpl.rcParams['axes.grid']and
792+
mpl.rcParams['axes.grid.which']in ('both','major'))
793+
794+
def_reset_minor_tick_kw(self):
795+
self._minor_tick_kw['gridOn']= (
796+
mpl.rcParams['axes.grid']and
797+
mpl.rcParams['axes.grid.which']in ('both','minor'))
798+
789799
defclear(self):
790800
"""
791801
Clear the axis.
@@ -807,14 +817,8 @@ def clear(self):
807817
# Clear the callback registry for this axis, or it may "leak"
808818
self.callbacks=cbook.CallbackRegistry()
809819

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-
820+
self._reset_major_tick_kw()
821+
self._reset_minor_tick_kw()
818822
self.reset_ticks()
819823

820824
self.converter=None
@@ -861,10 +865,10 @@ def set_tick_params(self, which='major', reset=False, **kw):
861865
# future new ticks will automatically get them
862866
ifreset:
863867
ifwhichin ['major','both']:
864-
self._major_tick_kw.clear()
868+
self._reset_major_tick_kw()
865869
self._major_tick_kw.update(kwtrans)
866870
ifwhichin ['minor','both']:
867-
self._minor_tick_kw.clear()
871+
self._reset_minor_tick_kw()
868872
self._minor_tick_kw.update(kwtrans)
869873
self.reset_ticks()
870874
else:
@@ -1413,6 +1417,9 @@ def grid(self, b=None, which='major', **kwargs):
14131417
14141418
grid(color='r', linestyle='-', linewidth=2)
14151419
"""
1420+
ifbisNoneandkwargs:
1421+
b=True# if any kwargs are supplied, assume you want the grid on
1422+
14161423
ifbisnotNone:
14171424
if'visible'inkwargsandbool(b)!=bool(kwargs['visible']):
14181425
raiseValueError(

‎lib/matplotlib/tests/test_axes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4757,6 +4757,19 @@ def test_grid():
47574757
assertnotax.xaxis.majorTicks[0].gridline.get_visible()
47584758

47594759

4760+
deftest_reset_grid():
4761+
fig,ax=plt.subplots()
4762+
ax.tick_params(reset=True,which='major',labelsize=10)
4763+
assertnotax.xaxis.majorTicks[0].gridline.get_visible()
4764+
ax.grid(color='red')# enables grid
4765+
assertax.xaxis.majorTicks[0].gridline.get_visible()
4766+
4767+
withplt.rc_context({'axes.grid':True}):
4768+
ax.clear()
4769+
ax.tick_params(reset=True,which='major',labelsize=10)
4770+
assertax.xaxis.majorTicks[0].gridline.get_visible()
4771+
4772+
47604773
deftest_vline_limit():
47614774
fig=plt.figure()
47624775
ax=fig.gca()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp