From 324a8abaa3926955325b5ad567abf59676556f62 Mon Sep 17 00:00:00 2001 From: Casey Brooks Date: Thu, 25 Dec 2025 12:28:56 +0000 Subject: [PATCH] fix(axis): respect tick labelcolor References: #51 Task: 276 --- lib/matplotlib/axis.py | 10 ++++- lib/matplotlib/tests/test_axes.py | 74 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index c0e706e2b3a1..4e9d8b70271a 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2249,13 +2249,16 @@ def _init(self): ) self.label_position = 'bottom' + labelcolor = mpl.rcParams['xtick.labelcolor'] + offset_color = (labelcolor if labelcolor != 'inherit' + else mpl.rcParams['xtick.color']) self.offsetText.set( x=1, y=0, verticalalignment='top', horizontalalignment='right', transform=mtransforms.blended_transform_factory( self.axes.transAxes, mtransforms.IdentityTransform()), fontsize=mpl.rcParams['xtick.labelsize'], - color=mpl.rcParams['xtick.color'], + color=offset_color, ) self.offset_text_position = 'bottom' @@ -2509,13 +2512,16 @@ def _init(self): ) self.label_position = 'left' # x in axes coords, y in display coords(!). + labelcolor = mpl.rcParams['ytick.labelcolor'] + offset_color = (labelcolor if labelcolor != 'inherit' + else mpl.rcParams['ytick.color']) self.offsetText.set( x=0, y=0.5, verticalalignment='baseline', horizontalalignment='left', transform=mtransforms.blended_transform_factory( self.axes.transAxes, mtransforms.IdentityTransform()), fontsize=mpl.rcParams['ytick.labelsize'], - color=mpl.rcParams['ytick.color'], + color=offset_color, ) self.offset_text_position = 'left' diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 8483ed077bdf..78a66c2248c3 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6068,6 +6068,80 @@ def test_move_offsetlabel(): assert ax.xaxis.offsetText.get_verticalalignment() == 'bottom' +def _axes_with_scientific_offsets(): + fig, ax = plt.subplots() + x = np.linspace(1e10, 1e10 + 1, 3) + y = np.linspace(1e12, 1e12 + 1, 3) + ax.plot(x, y) + ax.ticklabel_format(style='sci', axis='both', scilimits=(0, 0)) + fig.canvas.draw() + assert ax.xaxis.offsetText.get_text() + assert ax.yaxis.offsetText.get_text() + return fig, ax + + +def test_offset_text_uses_labelcolor_from_rc(): + rc = { + 'xtick.labelcolor': 'red', + 'ytick.labelcolor': 'green', + 'xtick.color': 'black', + 'ytick.color': 'black', + } + with matplotlib.rc_context(rc): + fig, ax = _axes_with_scientific_offsets() + try: + assert ( + mcolors.to_rgba(ax.xaxis.offsetText.get_color()) + == mcolors.to_rgba('red') + ) + assert ( + mcolors.to_rgba(ax.yaxis.offsetText.get_color()) + == mcolors.to_rgba('green') + ) + finally: + plt.close(fig) + + +def test_offset_text_inherits_tick_color_when_labelcolor_inherit(): + rc = { + 'xtick.labelcolor': 'inherit', + 'ytick.labelcolor': 'inherit', + 'xtick.color': 'blue', + 'ytick.color': 'magenta', + } + with matplotlib.rc_context(rc): + fig, ax = _axes_with_scientific_offsets() + try: + assert ( + mcolors.to_rgba(ax.xaxis.offsetText.get_color()) + == mcolors.to_rgba('blue') + ) + assert ( + mcolors.to_rgba(ax.yaxis.offsetText.get_color()) + == mcolors.to_rgba('magenta') + ) + finally: + plt.close(fig) + + +def test_offset_text_follows_tick_params_labelcolor(): + fig, ax = _axes_with_scientific_offsets() + try: + ax.tick_params(axis='x', labelcolor='purple') + ax.tick_params(axis='y', labelcolor='orange') + fig.canvas.draw() + assert ( + mcolors.to_rgba(ax.xaxis.offsetText.get_color()) + == mcolors.to_rgba('purple') + ) + assert ( + mcolors.to_rgba(ax.yaxis.offsetText.get_color()) + == mcolors.to_rgba('orange') + ) + finally: + plt.close(fig) + + @image_comparison(['rc_spines.png'], savefig_kwarg={'dpi': 40}) def test_rc_spines(): rc_dict = {