diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 1295d351d0b8..d56af88b64f6 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2047,19 +2047,15 @@ def _update_offset_text_position(self, bboxes, bboxes2): else: bbox = mtransforms.Bbox.union(bboxes) bottom = bbox.y0 - self.offsetText.set_position( - (x, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72) - ) + y = bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72 else: if not len(bboxes2): top = self.axes.bbox.ymax else: bbox = mtransforms.Bbox.union(bboxes2) top = bbox.y1 - self.offsetText.set_va('top') - self.offsetText.set_position( - (x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72) - ) + y = top + self.OFFSETTEXTPAD * self.figure.dpi / 72 + self.offsetText.set_position((x, y)) def get_text_heights(self, renderer): """ @@ -2102,10 +2098,12 @@ def set_ticks_position(self, position): self.set_tick_params(which='both', top=True, labeltop=True, bottom=False, labelbottom=False) self._tick_position = 'top' + self.offsetText.set_verticalalignment('bottom') elif position == 'bottom': self.set_tick_params(which='both', top=False, labeltop=False, bottom=True, labelbottom=True) self._tick_position = 'bottom' + self.offsetText.set_verticalalignment('top') elif position == 'both': self.set_tick_params(which='both', top=True, bottom=True) @@ -2116,6 +2114,7 @@ def set_ticks_position(self, position): self.set_tick_params(which='both', top=True, labeltop=False, bottom=True, labelbottom=True) self._tick_position = 'bottom' + self.offsetText.set_verticalalignment('top') else: assert False, "unhandled parameter not caught by _check_in_list" self.stale = True diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9e12dd95b385..986caba0dd01 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4641,19 +4641,23 @@ def test_move_offsetlabel(): ax.plot(data) fig.canvas.draw() before = ax.yaxis.offsetText.get_position() + assert ax.yaxis.offsetText.get_horizontalalignment() == 'left' ax.yaxis.tick_right() fig.canvas.draw() after = ax.yaxis.offsetText.get_position() assert after[0] > before[0] and after[1] == before[1] + assert ax.yaxis.offsetText.get_horizontalalignment() == 'right' fig, ax = plt.subplots() ax.plot(data) fig.canvas.draw() before = ax.xaxis.offsetText.get_position() + assert ax.xaxis.offsetText.get_verticalalignment() == 'top' ax.xaxis.tick_top() fig.canvas.draw() after = ax.xaxis.offsetText.get_position() assert after[0] == before[0] and after[1] > before[1] + assert ax.xaxis.offsetText.get_verticalalignment() == 'bottom' @image_comparison(['rc_spines.png'], savefig_kwarg={'dpi': 40})