Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64fdfcb

Browse files
committed
Fix alignment of offset text on top axis.
When set to show offset text on the top, the alignment is set to top, but that's the same alignment as if it were on the bottom. It should be set to bottom instead, or it will overlap with ticks.
1 parent 7b7017e commit 64fdfcb

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

lib/matplotlib/axis.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,19 +2047,15 @@ def _update_offset_text_position(self, bboxes, bboxes2):
20472047
else:
20482048
bbox = mtransforms.Bbox.union(bboxes)
20492049
bottom = bbox.y0
2050-
self.offsetText.set_position(
2051-
(x, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72)
2052-
)
2050+
y = bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72
20532051
else:
20542052
if not len(bboxes2):
20552053
top = self.axes.bbox.ymax
20562054
else:
20572055
bbox = mtransforms.Bbox.union(bboxes2)
20582056
top = bbox.y1
2059-
self.offsetText.set_va('top')
2060-
self.offsetText.set_position(
2061-
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
2062-
)
2057+
y = top + self.OFFSETTEXTPAD * self.figure.dpi / 72
2058+
self.offsetText.set_position((x, y))
20632059

20642060
def get_text_heights(self, renderer):
20652061
"""
@@ -2102,10 +2098,12 @@ def set_ticks_position(self, position):
21022098
self.set_tick_params(which='both', top=True, labeltop=True,
21032099
bottom=False, labelbottom=False)
21042100
self._tick_position = 'top'
2101+
self.offsetText.set_verticalalignment('bottom')
21052102
elif position == 'bottom':
21062103
self.set_tick_params(which='both', top=False, labeltop=False,
21072104
bottom=True, labelbottom=True)
21082105
self._tick_position = 'bottom'
2106+
self.offsetText.set_verticalalignment('top')
21092107
elif position == 'both':
21102108
self.set_tick_params(which='both', top=True,
21112109
bottom=True)
@@ -2116,6 +2114,7 @@ def set_ticks_position(self, position):
21162114
self.set_tick_params(which='both', top=True, labeltop=False,
21172115
bottom=True, labelbottom=True)
21182116
self._tick_position = 'bottom'
2117+
self.offsetText.set_verticalalignment('top')
21192118
else:
21202119
assert False, "unhandled parameter not caught by _check_in_list"
21212120
self.stale = True

lib/matplotlib/tests/test_axes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4641,19 +4641,23 @@ def test_move_offsetlabel():
46414641
ax.plot(data)
46424642
fig.canvas.draw()
46434643
before = ax.yaxis.offsetText.get_position()
4644+
assert ax.yaxis.offsetText.get_horizontalalignment() == 'left'
46444645
ax.yaxis.tick_right()
46454646
fig.canvas.draw()
46464647
after = ax.yaxis.offsetText.get_position()
46474648
assert after[0] > before[0] and after[1] == before[1]
4649+
assert ax.yaxis.offsetText.get_horizontalalignment() == 'right'
46484650

46494651
fig, ax = plt.subplots()
46504652
ax.plot(data)
46514653
fig.canvas.draw()
46524654
before = ax.xaxis.offsetText.get_position()
4655+
assert ax.xaxis.offsetText.get_verticalalignment() == 'top'
46534656
ax.xaxis.tick_top()
46544657
fig.canvas.draw()
46554658
after = ax.xaxis.offsetText.get_position()
46564659
assert after[0] == before[0] and after[1] > before[1]
4660+
assert ax.xaxis.offsetText.get_verticalalignment() == 'bottom'
46574661

46584662

46594663
@image_comparison(['rc_spines.png'], savefig_kwarg={'dpi': 40})

0 commit comments

Comments
 (0)