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

Skip to content

Commit 698397f

Browse files
authored
Merge pull request #22313 from jklymak/fix-colorbar-exponents
Fix colorbar exponents
2 parents 23cd7dd + 707d8a2 commit 698397f

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2975,7 +2975,11 @@ def _update_title_position(self, renderer):
29752975
or ax.xaxis.get_label_position() == 'top'):
29762976
bb = ax.xaxis.get_tightbbox(renderer)
29772977
if bb is None:
2978-
bb = ax.get_window_extent(renderer)
2978+
if 'outline' in ax.spines:
2979+
# Special case for colorbars:
2980+
bb = ax.spines['outline'].get_window_extent()
2981+
else:
2982+
bb = ax.get_window_extent(renderer)
29792983
top = max(top, bb.ymax)
29802984
if title.get_text():
29812985
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
@@ -2474,8 +2474,13 @@ def _update_offset_text_position(self, bboxes, bboxes2):
24742474
Update the offset_text position based on the sequence of bounding
24752475
boxes of all the ticklabels
24762476
"""
2477-
x, y = self.offsetText.get_position()
2478-
top = self.axes.bbox.ymax
2477+
x, _ = self.offsetText.get_position()
2478+
if 'outline' in self.axes.spines:
2479+
# Special case for colorbars:
2480+
bbox = self.axes.spines['outline'].get_window_extent()
2481+
else:
2482+
bbox = self.axes.bbox
2483+
top = bbox.ymax
24792484
self.offsetText.set_position(
24802485
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
24812486
)

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)

tutorials/introductory/quick_start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ def my_plotter(ax, data1, data2, param_dict):
505505

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

510510
##############################################################################
511511
# Colormaps

0 commit comments

Comments
 (0)