From c7c2cd94f10ffa5b90b42ccc8474ff1ed624da98 Mon Sep 17 00:00:00 2001 From: hhalwan Date: Sat, 16 Apr 2022 23:03:33 -0400 Subject: [PATCH] Allowed colors of tick labels to be automatically restored after call to spine.set_position set_position currently resets the color of tick labels after a call to reset_ticks. This commit saves the color and restores the color to the tick labels after reset is called. Also, reset_ticks is now only called when the position is not 'outward', 0.0. This is to prevent resetting and applying color during the subplot initialization. Also added a test case to check this. --- lib/matplotlib/spines.py | 10 +++++++++- lib/matplotlib/tests/test_spines.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/spines.py b/lib/matplotlib/spines.py index af8c60dece39..48b3dcb5773e 100644 --- a/lib/matplotlib/spines.py +++ b/lib/matplotlib/spines.py @@ -318,8 +318,16 @@ def set_position(self, position): "'axes', or 'data' ") self._position = position self.set_transform(self.get_spine_transform()) - if self.axis is not None: + if self.axis is not None and position != ('outward', 0.0): + major_labels = self.axis.get_majorticklabels() + minor_labels = self.axis.get_minorticklabels() + major_cols = [lab.get_color() for lab in major_labels] + minor_cols = [lab.get_color() for lab in minor_labels] self.axis.reset_ticks() + for col, label in zip(major_cols, self.axis.get_majorticklabels()): + label.set_color(col) + for col, label in zip(minor_cols, self.axis.get_minorticklabels()): + label.set_color(col) self.stale = True def get_position(self): diff --git a/lib/matplotlib/tests/test_spines.py b/lib/matplotlib/tests/test_spines.py index b8891aec411e..ad9cd3037cb0 100644 --- a/lib/matplotlib/tests/test_spines.py +++ b/lib/matplotlib/tests/test_spines.py @@ -134,3 +134,19 @@ def test_label_without_ticks(): spine.get_path()).get_extents() assert ax.xaxis.label.get_position()[1] < spinebbox.ymin, \ "X-Axis label not below the spine" + + +@check_figures_equal(extensions=['png']) +def test_set_position_maintains_label_color(fig_test, fig_ref): + # set_position should not change the label color + ax = fig_test.subplots() + ax.plot([1, 2, 3]) + for lab in ax.get_xticklabels(): + lab.set_color("r") + ax.spines.left.set_position(("outward", 10)) + + ax = fig_ref.subplots() + ax.plot([1, 2, 3]) + ax.spines.left.set_position(("outward", 10)) + for lab in ax.get_xticklabels(): + lab.set_color("r")