BUG: Fix IndexLocator.tick_values returning values greater than vmax#31091
Merged
timhoffm merged 5 commits intomatplotlib:mainfrom Feb 6, 2026
Merged
Conversation
Fix IndexLocator.tick_values() to not return tick values that exceed vmax. Previously, when using IndexLocator with certain offset values, the generated ticks could exceed the specified vmax, causing issues like colorbar.get_ticks() returning incorrect values for discrete colormaps with NoNorm normalization. The fix adds a filter to remove any tick values greater than vmax after generating the initial tick positions. Fixes matplotlib#31086
scottshambaugh
approved these changes
Feb 5, 2026
Contributor
scottshambaugh
left a comment
There was a problem hiding this comment.
Looks good to me, thanks!
timhoffm
reviewed
Feb 5, 2026
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com>
Contributor
Author
Makes sense. Thanks for the suggestions! committed them :) |
Use assert_array_equal directly for the fractional offset and base > 1 test cases, matching the style of the first test case.
timhoffm
approved these changes
Feb 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Fix IndexLocator.tick_values returning values greater than vmax
Summary
This PR fixes a bug in
IndexLocator.tick_values()where tick values could exceed the specifiedvmaxparameter. This issue manifested most notably when using colorbars with discrete colormaps andNoNormnormalization, wherecbar.get_ticks()would return an incorrect array containing values outside the valid range.The Problem
When creating a colorbar using a
ScalarMappablewithNoNormnormalization and a discrete colormap (e.g., viridis with a fixed number of colors), the ticks returned bycolorbar.get_ticks()did not align with the visually displayed tick positions. Specifically, callingcbar.set_ticks(cbar.get_ticks())would unexpectedly change the ticks becauseget_ticks()returned values outside the valid range.Example reproduction:
Root Cause
The issue was in
IndexLocator.tick_values()inlib/matplotlib/ticker.py:The
vmax + 1in thenp.arangecall was intended to make the interval inclusive ofvmax(sincenp.arangeproduces a semi-open interval[start, stop)). However, this overshot the target — combined with certain offset values, it could generate tick positions that exceedvmax. For instance, withoffset=0.5andvmax=4, the array[0.5, 1.5, 2.5, 3.5, 4.5]would be generated, where4.5 > vmax.The Fix
Replaced
vmax + 1withvmax + 1e-12as the stop value fornp.arange. This provides a minimal epsilon beyondvmaxthat ensuresvmaxitself is included in the closed interval[vmin, vmax], without overshooting to produce values beyondvmax:This directly generates the correct number of ticks without any post-filtering, which is both cleaner and more efficient.
Changes
Modified Files
lib/matplotlib/ticker.pyIndexLocator.tick_values()to usevmax + 1e-12instead ofvmax + 1as thenp.arangestop value, ensuring tick values stay within the[vmin, vmax]closed intervallib/matplotlib/tests/test_ticker.pytest_tick_values_not_exceeding_vmax()test method toTestIndexLocatorclassoffset=0)offset=0.5)base=2)Test Plan
test_ticker.pypytest lib/matplotlib/tests/test_ticker.py -vNew Test Cases
Related Issues
Checklist