Skip to content

Fix colorbar exponents #22313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/matplotlib/axes/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2975,7 +2975,11 @@ def _update_title_position(self, renderer):
or ax.xaxis.get_label_position() == 'top'):
bb = ax.xaxis.get_tightbbox(renderer)
if bb is None:
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)
top = max(top, bb.ymax)
if title.get_text():
ax.yaxis.get_tightbbox(renderer) # update offsetText
Expand Down
9 changes: 7 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2474,8 +2474,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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to update the axes.bbox in Colorbar to be the full spines outline? Move it up a level, so everything is referencing the same full bbox rather than just the text here. I'm not sure if that would mess up the tickers to draw in the extends region or not then...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ax.transAxes comes from the ax.bbox, so I don't think that would work as the axes extends are drawn in transAxes. I appreciate what you are saying though, it is a bit of a hack to special-case colorbars here...

Another alternative is to turn the axis off, use ax.get_tightbbox() and then turn the axis back on again. But I think get_tightbbox can get pretty expensive depending on artists. This is quite fast...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to paraphrase: the colorbar axes has additional decorations which are outside the bbox but need to be taken into accout for shifting the offset text? In that case, do we need a concept for "bbox with decorations"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do, it's called get_tightbbox. However that is defined on the axes and takes into account the axis, and this exponent is part of the axis, so you get an infinite recursion. I guess we could change get_tightbbox to accept a list of artists to exclude, but I'm not convinced that is any better than this solution. And again get_tightbbox can be very expensive.

Copy link
Member

@timhoffm timhoffm Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. I don't claim to I know how the correct or practical solution. I was just trying to understand what's going on.

top = bbox.ymax
self.offsetText.set_position(
(x, top + self.OFFSETTEXTPAD * self.figure.dpi / 72)
)
Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,3 +979,30 @@ def test_colorbar_set_formatter_locator():
fmt = LogFormatter()
cb.minorformatter = fmt
assert cb.ax.yaxis.get_minor_formatter() is fmt


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)
2 changes: 1 addition & 1 deletion tutorials/introductory/quick_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,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()')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these semicolons dont' work anyways, so removed it here...


##############################################################################
# Colormaps
Expand Down