From f6c755c064b17569a1856ecf8601046ab1eec079 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 8 Mar 2022 19:13:58 -0500 Subject: [PATCH] Backport PR #22313: Fix colorbar exponents --- lib/matplotlib/axes/_base.py | 6 +++++- lib/matplotlib/axis.py | 9 +++++++-- lib/matplotlib/tests/test_colorbar.py | 27 +++++++++++++++++++++++++++ tutorials/introductory/usage.py | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index cd966ee45b42..2a95e5c60c4e 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2990,7 +2990,11 @@ def _update_title_position(self, renderer): or ax.xaxis.get_label_position() == 'top'): bb = ax.xaxis.get_tightbbox(renderer) else: - bb = ax.get_window_extent(renderer) + if 'outline' in ax.spines: + # Special case for colorbars: + bb = ax.spines['outline'].get_window_extent() + else: + bb = ax.get_window_extent(renderer) if bb is not None: top = max(top, bb.ymax) if top < 0: diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 74d3054864be..10a6c7f747ba 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -2380,8 +2380,13 @@ def _update_offset_text_position(self, bboxes, bboxes2): Update the offset_text position based on the sequence of bounding boxes of all the ticklabels """ - x, y = self.offsetText.get_position() - top = self.axes.bbox.ymax + x, _ = self.offsetText.get_position() + if 'outline' in self.axes.spines: + # Special case for colorbars: + bbox = self.axes.spines['outline'].get_window_extent() + else: + bbox = self.axes.bbox + top = bbox.ymax self.offsetText.set_position( (x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72) ) diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 29aa18b8596a..304056f6d190 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -968,3 +968,30 @@ def test_boundaries(): fig, ax = plt.subplots(figsize=(2, 2)) pc = ax.pcolormesh(np.random.randn(10, 10), cmap='RdBu_r') cb = fig.colorbar(pc, ax=ax, boundaries=np.linspace(-3, 3, 7)) + + +def test_offset_text_loc(): + plt.style.use('mpl20') + fig, ax = plt.subplots() + np.random.seed(seed=19680808) + pc = ax.pcolormesh(np.random.randn(10, 10)*1e6) + cb = fig.colorbar(pc, location='right', extend='max') + fig.draw_without_rendering() + # check that the offsetText is in the proper place above the + # colorbar axes. In this case the colorbar axes is the same + # height as the parent, so use the parents bbox. + assert cb.ax.yaxis.offsetText.get_position()[1] > ax.bbox.y1 + + +def test_title_text_loc(): + plt.style.use('mpl20') + fig, ax = plt.subplots() + np.random.seed(seed=19680808) + pc = ax.pcolormesh(np.random.randn(10, 10)) + cb = fig.colorbar(pc, location='right', extend='max') + cb.ax.set_title('Aardvark') + fig.draw_without_rendering() + # check that the title is in the proper place above the + # colorbar axes, including its extend triangles.... + assert (cb.ax.title.get_window_extent(fig.canvas.get_renderer()).ymax > + cb.ax.spines['outline'].get_window_extent().ymax) diff --git a/tutorials/introductory/usage.py b/tutorials/introductory/usage.py index bccb8602f511..2de9a2798683 100644 --- a/tutorials/introductory/usage.py +++ b/tutorials/introductory/usage.py @@ -502,7 +502,7 @@ def my_plotter(ax, data1, data2, param_dict): pc = axs[1, 1].scatter(data1, data2, c=data3, cmap='RdBu_r') fig.colorbar(pc, ax=axs[1, 1], extend='both') -axs[1, 1].set_title('scatter()'); +axs[1, 1].set_title('scatter()') ############################################################################## # Colormaps