Skip to content
Open
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
10 changes: 8 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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'

Expand Down
74 changes: 74 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down