Skip to content

Commit 012f9d5

Browse files
committed
Fix offsetText color to respect tick.labelcolor
Previously, offsetText for axes used tick.color instead of tick.labelcolor, leading to inconsistent label coloring. Now, offsetText uses tick.labelcolor unless it is set to 'inherit', in which case it falls back to tick.color. Adds tests to verify correct color inheritance and fix matplotlib#25266.
1 parent f8ffce6 commit 012f9d5

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎lib/matplotlib/axis.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ def _init(self):
22552255
transform=mtransforms.blended_transform_factory(
22562256
self.axes.transAxes, mtransforms.IdentityTransform()),
22572257
fontsize=mpl.rcParams['xtick.labelsize'],
2258-
color=mpl.rcParams['xtick.color'],
2258+
color=mpl.rcParams['xtick.color'] if mpl.rcParams['xtick.labelcolor'] == 'inherit' else mpl.rcParams['xtick.labelcolor'],
22592259
)
22602260
self.offset_text_position = 'bottom'
22612261

@@ -2515,7 +2515,7 @@ def _init(self):
25152515
transform=mtransforms.blended_transform_factory(
25162516
self.axes.transAxes, mtransforms.IdentityTransform()),
25172517
fontsize=mpl.rcParams['ytick.labelsize'],
2518-
color=mpl.rcParams['ytick.color'],
2518+
color=mpl.rcParams['ytick.color'] if mpl.rcParams['ytick.labelcolor'] == 'inherit' else mpl.rcParams['ytick.labelcolor'],
25192519
)
25202520
self.offset_text_position = 'left'
25212521

‎lib/matplotlib/tests/test_axes.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7811,6 +7811,48 @@ def test_ytickcolor_is_not_yticklabelcolor():
78117811
assert tick.label1.get_color() == 'blue'
78127812

78137813

7814+
def test_xaxis_offsetText_color():
7815+
"""Test that xaxis offsetText respects xtick.labelcolor."""
7816+
plt.rcParams['xtick.color'] = 'yellow'
7817+
plt.rcParams['xtick.labelcolor'] = 'blue'
7818+
ax = plt.axes()
7819+
# Force the creation of offsetText by using large numbers
7820+
ax.plot([1e9, 2e9, 3e9], [1, 2, 3])
7821+
offset_text = ax.xaxis.get_offset_text()
7822+
assert offset_text.get_color() == 'blue'
7823+
7824+
7825+
def test_yaxis_offsetText_color():
7826+
"""Test that yaxis offsetText respects ytick.labelcolor."""
7827+
plt.rcParams['ytick.color'] = 'yellow'
7828+
plt.rcParams['ytick.labelcolor'] = 'blue'
7829+
ax = plt.axes()
7830+
# Force the creation of offsetText by using large numbers
7831+
ax.plot([1, 2, 3], [1e9, 2e9, 3e9])
7832+
offset_text = ax.yaxis.get_offset_text()
7833+
assert offset_text.get_color() == 'blue'
7834+
7835+
7836+
def test_xaxis_offsetText_color_inherit():
7837+
"""Test that xaxis offsetText inherits from xtick.color when labelcolor='inherit'."""
7838+
plt.rcParams['xtick.color'] = 'green'
7839+
plt.rcParams['xtick.labelcolor'] = 'inherit'
7840+
ax = plt.axes()
7841+
ax.plot([1e9, 2e9, 3e9], [1, 2, 3])
7842+
offset_text = ax.xaxis.get_offset_text()
7843+
assert offset_text.get_color() == 'green'
7844+
7845+
7846+
def test_yaxis_offsetText_color_inherit():
7847+
"""Test that yaxis offsetText inherits from ytick.color when labelcolor='inherit'."""
7848+
plt.rcParams['ytick.color'] = 'green'
7849+
plt.rcParams['ytick.labelcolor'] = 'inherit'
7850+
ax = plt.axes()
7851+
ax.plot([1, 2, 3], [1e9, 2e9, 3e9])
7852+
offset_text = ax.yaxis.get_offset_text()
7853+
assert offset_text.get_color() == 'green'
7854+
7855+
78147856
@pytest.mark.parametrize('size', [size for size in mfont_manager.font_scalings
78157857
if size is not None] + [8, 10, 12])
78167858
@mpl.style.context('default')

0 commit comments

Comments
 (0)