Skip to content

Make singular colorbars consistent with single-value mappables. #26307

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/behavior/26307-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Colorbars of single-value norms now map that value to the lowest color, not the midpoint color
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This behavior is consistent with e.g. how `~.Axes.imshow` handle inputs with
``vmin == vmax``.
9 changes: 7 additions & 2 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,8 +1088,13 @@ def _process_values(self):
# If we still aren't scaled after autoscaling, use 0, 1 as default
self.norm.vmin = 0
self.norm.vmax = 1
self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
self.norm.vmin, self.norm.vmax, expander=0.1)

# Only expand vmax if needed, to match Normalize's behavior of mapping
# everything to 0 if the norm is singular.
vmin, vmax = sorted([self.norm.vmin, self.norm.vmax])
self.norm.vmin = vmin
_, self.norm.vmax = mtransforms.nonsingular(vmin, vmax, expander=0.1)
Comment on lines +1094 to +1096
Copy link
Contributor

Choose a reason for hiding this comment

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

This forces self.norm.vmin <= self.norm.vmax now I think. Would a user care if they had defined their norm reversed and we are now swapping this for them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The previous version (just calling nonsingular) already performed that ordering; I'm not changing that behavior (which I agree we can question), just doing it explicitly first to make it easier to just expand on the top side.


if (not isinstance(self.norm, colors.BoundaryNorm) and
(self.boundaries is None)):
b = self.norm.inverse(b)
Expand Down
Binary file not shown.
17 changes: 8 additions & 9 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,17 @@ def test_gridspec_make_colorbar():
plt.subplots_adjust(top=0.95, right=0.95, bottom=0.2, hspace=0.25)


@image_comparison(['colorbar_single_scatter.png'], remove_text=True,
savefig_kwarg={'dpi': 40})
def test_colorbar_single_scatter():
# Issue #2642: if a path collection has only one entry,
# the norm scaling within the colorbar must ensure a
# finite range, otherwise a zero denominator will occur in _locate.
# Issue #2642: If a path collection has only one entry, the norm scaling within the
# colorbar must ensure a finite range, otherwise a zero denominator will occur in
# _locate. Also, adding the colorbar must not change the value's normalization.
plt.figure()
x = y = [0]
z = [50]
cmap = mpl.colormaps['jet'].resampled(16)
cs = plt.scatter(x, y, z, c=z, cmap=cmap)
v = 50
cs = plt.scatter([0], [0], c=[50])
old_normed = cs.norm(v)
plt.colorbar(cs)
new_normed = cs.norm(v)
assert old_normed == new_normed


@pytest.mark.parametrize('use_gridspec', [True, False])
Expand Down