Skip to content

Commit f6c755c

Browse files
committed
Backport PR #22313: Fix colorbar exponents
1 parent f9f736a commit f6c755c

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

lib/matplotlib/axes/_base.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2990,7 +2990,11 @@ def _update_title_position(self, renderer):
29902990
or ax.xaxis.get_label_position() == 'top'):
29912991
bb = ax.xaxis.get_tightbbox(renderer)
29922992
else:
2993-
bb = ax.get_window_extent(renderer)
2993+
if 'outline' in ax.spines:
2994+
# Special case for colorbars:
2995+
bb = ax.spines['outline'].get_window_extent()
2996+
else:
2997+
bb = ax.get_window_extent(renderer)
29942998
if bb is not None:
29952999
top = max(top, bb.ymax)
29963000
if top < 0:

lib/matplotlib/axis.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -2380,8 +2380,13 @@ def _update_offset_text_position(self, bboxes, bboxes2):
23802380
Update the offset_text position based on the sequence of bounding
23812381
boxes of all the ticklabels
23822382
"""
2383-
x, y = self.offsetText.get_position()
2384-
top = self.axes.bbox.ymax
2383+
x, _ = self.offsetText.get_position()
2384+
if 'outline' in self.axes.spines:
2385+
# Special case for colorbars:
2386+
bbox = self.axes.spines['outline'].get_window_extent()
2387+
else:
2388+
bbox = self.axes.bbox
2389+
top = bbox.ymax
23852390
self.offsetText.set_position(
23862391
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
23872392
)

lib/matplotlib/tests/test_colorbar.py

+27
Original file line numberDiff line numberDiff line change
@@ -968,3 +968,30 @@ def test_boundaries():
968968
fig, ax = plt.subplots(figsize=(2, 2))
969969
pc = ax.pcolormesh(np.random.randn(10, 10), cmap='RdBu_r')
970970
cb = fig.colorbar(pc, ax=ax, boundaries=np.linspace(-3, 3, 7))
971+
972+
973+
def test_offset_text_loc():
974+
plt.style.use('mpl20')
975+
fig, ax = plt.subplots()
976+
np.random.seed(seed=19680808)
977+
pc = ax.pcolormesh(np.random.randn(10, 10)*1e6)
978+
cb = fig.colorbar(pc, location='right', extend='max')
979+
fig.draw_without_rendering()
980+
# check that the offsetText is in the proper place above the
981+
# colorbar axes. In this case the colorbar axes is the same
982+
# height as the parent, so use the parents bbox.
983+
assert cb.ax.yaxis.offsetText.get_position()[1] > ax.bbox.y1
984+
985+
986+
def test_title_text_loc():
987+
plt.style.use('mpl20')
988+
fig, ax = plt.subplots()
989+
np.random.seed(seed=19680808)
990+
pc = ax.pcolormesh(np.random.randn(10, 10))
991+
cb = fig.colorbar(pc, location='right', extend='max')
992+
cb.ax.set_title('Aardvark')
993+
fig.draw_without_rendering()
994+
# check that the title is in the proper place above the
995+
# colorbar axes, including its extend triangles....
996+
assert (cb.ax.title.get_window_extent(fig.canvas.get_renderer()).ymax >
997+
cb.ax.spines['outline'].get_window_extent().ymax)

tutorials/introductory/usage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ def my_plotter(ax, data1, data2, param_dict):
502502

503503
pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r')
504504
fig.colorbar(pc, ax=axs[1, 1], extend='both')
505-
axs[1, 1].set_title('scatter()');
505+
axs[1, 1].set_title('scatter()')
506506

507507
##############################################################################
508508
# Colormaps

0 commit comments

Comments
 (0)