8000 Offset text position (#15592) · matplotlib/matplotlib@74a0e9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 74a0e9b

Browse files
MarcoGorellitimhoffm
authored andcommitted
Offset text position (#15592)
* Rewrite tests so they don't require saved png files * Add internal flag _text_offset_position, use it to update offset text position * Remove unnecessary .set_va * Fix style (as per review), as default behaviour * Add whatsnew entry
1 parent 1e043bc commit 74a0e9b

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Offset text is now set to the top when using axis.tick_top()
2+
------------------------------------------------------------
3+
4+
Solves the issue that the power indicator (e.g. 1e4) stayed on the bottom, even if the ticks were on the top.

lib/matplotlib/axis.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2060,14 +2060,27 @@ def _update_offset_text_position(self, bboxes, bboxes2):
20602060
boxes of all the ticklabels
20612061
"""
20622062
x, y = self.offsetText.get_position()
2063-
if not len(bboxes):
2064-
bottom = self.axes.bbox.ymin
2063+
if not hasattr(self, '_tick_position'):
2064+
self._tick_position = 'bottom'
2065+
if self._tick_position == 'bottom':
2066+
if not len(bboxes):
2067+
bottom = self.axes.bbox.ymin
2068+
else:
2069+
bbox = mtransforms.Bbox.union(bboxes)
2070+
bottom = bbox.y0
2071+
self.offsetText.set_position(
2072+
(x, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72)
2073+
)
20652074
else:
2066-
bbox = mtransforms.Bbox.union(bboxes)
2067-
bottom = bbox.y0
2068-
self.offsetText.set_position(
2069-
(x, bottom - self.OFFSETTEXTPAD * self.figure.dpi / 72)
2070-
)
2075+
if not len(bboxes2):
2076+
top = self.axes.bbox.ymax
2077+
else:
2078+
bbox = mtransforms.Bbox.union(bboxes2)
2079+
top = bbox.y1
2080+
self.offsetText.set_va('top')
2081+
self.offsetText.set_position(
2082+
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
2083+
)
20712084

20722085
def get_text_heights(self, renderer):
20732086
"""
@@ -2107,9 +2120,11 @@ def set_ticks_position(self, position):
21072120
if position == 'top':
21082121
self.set_tick_params(which='both', top=True, labeltop=True,
21092122
bottom=False, labelbottom=False)
2123+
self._tick_position = 'top'
21102124
elif position == 'bottom':
21112125
self.set_tick_params(which='both', top=False, labeltop=False,
21122126
bottom=True, labelbottom=True)
2127+
self._tick_position = 'bottom'
21132128
elif position == 'both':
21142129
self.set_tick_params(which='both', top=True,
21152130
bottom=True)
@@ -2119,6 +2134,7 @@ def set_ticks_position(self, position):
21192134
elif position == 'default':
21202135
self.set_tick_params(which='both', top=True, labeltop=False,
21212136
bottom=True, labelbottom=True)
2137+
self._tick_position = 'bottom'
21222138
else:
21232139
raise ValueError("invalid position: %s" % position)
21242140
self.stale = True

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5201,10 +5201,24 @@ def test_unicode_hist_label():
52015201

52025202
def test_move_offsetlabel():
52035203
data = np.random.random(10) * 1e-22
5204+
52045205
fig, ax = plt.subplots()
52055206
ax.plot(data)
5207+
fig.canvas.draw()
5208+
before = ax.yaxis.offsetText.get_position()
52065209
ax.yaxis.tick_right()
5207-
assert (1, 0.5) == ax.yaxis.offsetText.get_position()
5210+
fig.canvas.draw()
5211+
after = ax.yaxis.offsetText.get_position()
5212+
assert after[0] > before[0] and after[1] == before[1]
5213+
5214+
fig, ax = plt.subplots()
5215+
ax.plot(data)
5216+
fig.canvas.draw()
5217+
before = ax.xaxis.offsetText.get_position()
5218+
ax.xaxis.tick_top()
5219+
fig.canvas.draw()
5220+
after = ax.xaxis.offsetText.get_position()
5221+
assert after[0] == before[0] and after[1] > before[1]
52085222

52095223

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

0 commit comments

Comments
 (0)
0