Skip to content

Commit 4c371d0

Browse files
committed
FIX: fix offset text for vertical colorbars
FIX: title as well
1 parent 447160e commit 4c371d0

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3003,7 +3003,11 @@ def _update_title_position(self, renderer):
30033003
or ax.xaxis.get_label_position() == 'top'):
30043004
bb = ax.xaxis.get_tightbbox(renderer)
30053005
if bb is None:
3006-
bb = ax.get_window_extent(renderer)
3006+
if 'outline' in ax.spines:
3007+
# Special case for colorbars:
3008+
bb = self.axes.spines['outline'].get_window_extent()
3009+
else:
3010+
bb = ax.get_window_extent(renderer)
30073011
top = max(top, bb.ymax)
30083012
if title.get_text():
30093013
ax.yaxis.get_tightbbox(renderer) # update offsetText

lib/matplotlib/axis.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,13 @@ def _update_offset_text_position(self, bboxes, bboxes2):
23852385
Update the offset_text position based on the sequence of bounding
23862386
boxes of all the ticklabels
23872387
"""
2388-
x, y = self.offsetText.get_position()
2389-
top = self.axes.bbox.ymax
2388+
x, _ = self.offsetText.get_position()
2389+
if 'outline' in self.axes.spines:
2390+
# Special case for colorbars:
2391+
bbox = self.axes.spines['outline'].get_window_extent()
2392+
else:
2393+
bbox = self.axes.bbox
2394+
top = bbox.ymax
23902395
self.offsetText.set_position(
23912396
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
23922397
)

lib/matplotlib/tests/test_colorbar.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,3 +979,30 @@ def test_colorbar_set_formatter_locator():
979979
fmt = LogFormatter()
980980
cb.minorformatter = fmt
981981
assert cb.ax.yaxis.get_minor_formatter() is fmt
982+
983+
984+
def test_offset_text_loc():
985+
plt.style.use('mpl20')
986+
fig, ax = plt.subplots()
987+
np.random.seed(seed=19680808)
988+
pc = ax.pcolormesh(np.random.randn(10, 10)*1e6)
989+
cb = fig.colorbar(pc, location='right', extend='max')
990+
fig.draw_without_rendering()
991+
# check that the offsetText is in the proper place above the
992+
# colorbar axes. In this case the colorbar axes is the same
993+
# height as the parent, so use the parents bbox.
994+
assert cb.ax.yaxis.offsetText.get_position()[1] > ax.bbox.y1
995+
996+
997+
def test_title_text_loc():
998+
plt.style.use('mpl20')
999+
fig, ax = plt.subplots()
1000+
np.random.seed(seed=19680808)
1001+
pc = ax.pcolormesh(np.random.randn(10, 10))
1002+
cb = fig.colorbar(pc, location='right', extend='max')
1003+
cb.ax.set_title('Aardvark')
1004+
fig.draw_without_rendering()
1005+
# check that the title is in the proper place above the
1006+
# colorbar axes, including its extend triangles....
1007+
assert (cb.ax.title.get_window_extent(fig.canvas.get_renderer()).ymax >
1008+
cb.ax.spines['outline'].get_window_extent().ymax)

0 commit comments

Comments
 (0)