Skip to content

BUG: fix a regression where tick direction wasn't conserved by Axis.clear() #23808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,13 @@ def get_children(self):
return [self.label, self.offsetText,
*self.get_major_ticks(), *self.get_minor_ticks()]

# style parameters that should be preserved by reset operations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better, but I'm a bit of a loss what we actually want.

What's the policy for e.g.

  • zorder
  • the grid parameters like grid_color

We should have an explicit statement on why we keep some parameters but not others.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally left out grid params because I have no opinion there, but I agree it should be discussed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zorder seemed to fit in the "visibility" theme, for which a consensus appears to have been reached. Please correct me if I am wrong.

_style_tick_params = [
'tick1On', 'tick2On', 'tickdir',
'label1On', 'label2On', 'labelsize', 'labelcolor', 'labelrotation',
'size', 'width', 'color', 'pad'
]

def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False):
"""
Reset major tick params to defaults.
Expand All @@ -855,7 +862,7 @@ def _reset_major_tick_kw(self, keep_tick_and_label_visibility=False):
*keep_tick_and_label_visibility*.
"""
backup = {name: value for name, value in self._major_tick_kw.items()
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
if name in self.__class__._style_tick_params}
self._major_tick_kw.clear()
if keep_tick_and_label_visibility:
self._major_tick_kw.update(backup)
Expand All @@ -872,7 +879,7 @@ def _reset_minor_tick_kw(self, keep_tick_and_label_visibility=False):
*keep_tick_and_label_visibility*.
"""
backup = {name: value for name, value in self._minor_tick_kw.items()
if name in ['tick1On', 'tick2On', 'label1On', 'label2On']}
if name in self.__class__._style_tick_params}
self._minor_tick_kw.clear()
if keep_tick_and_label_visibility:
self._minor_tick_kw.update(backup)
Expand Down
28 changes: 28 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7665,6 +7665,34 @@ def test_2dcolor_plot(fig_test, fig_ref):
axs[4].bar(np.arange(10), np.arange(10), color=color.reshape((1, -1)))


@pytest.mark.parametrize(
# separate tested parameters into two collision-free sets
# (e.g. avoid supplying *color* and *colors* at the same time)
'params',
[
dict(
direction='in', length=2, width=5, color="red", pad=0.4,
bottom=False, top=True, left=False, right=True,
labelrotation=90, labelsize=20,
),
dict(
colors="red",
bottom=True, top=True, left=True, right=True,
labelbottom=False, labeltop=True, labelleft=False, labelright=True,
)
]
)
@check_figures_equal(extensions=['png'])
def test_persistent_style_axes_clear(fig_test, fig_ref, params):
# see https://github.com/matplotlib/matplotlib/issues/23806
ax = fig_ref.subplots(1, 1)
ax.tick_params(which='both', axis='both', **params)

ax = fig_test.subplots(1, 1)
ax.tick_params(which='both', axis='both', **params)
ax.clear()


@check_figures_equal(extensions=['png'])
def test_shared_axes_clear(fig_test, fig_ref):
x = np.arange(0.0, 2*np.pi, 0.01)
Expand Down