Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fixed bilinear interpolation forSegmentedBivarColormap#30824

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ayshih wants to merge4 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromayshih:segmentedbivarcolormap_fix

Conversation

@ayshih
Copy link
Contributor

@ayshihayshih commentedDec 8, 2025
edited
Loading

PR summary

The lookup table generated bySegmentedBivarColormap is slightly inaccurate. Most notably, the darkest color in the lookup table is not as dark as the darkest color in the input, and the same goes for the brightest color. This has been glossed over in the tests due to the liberal use ofatol.

>>>frommatplotlib.cmimportbivar_cmaps>>>cmap=bivar_cmaps['BiOrangeBlue']>>>print(cmap.patch[0,0, :])[0.0.0.]>>>print(cmap.patch[-1,0, :])[1.0.50. ]>>>print(cmap.patch[-1,-1, :])[1.1.1.]>>>print(cmap.lut[0,0, :])[0.002441410.002422240.002441411.        ]>>>print(cmap.lut[-1,0, :])[0.998535160.500484370.002441411.        ]>>>print(cmap.lut[-1,-1, :])[0.998535160.998546750.998535161.        ]

SegmentedBivarColormap uses the image-plotting resampler (Agg) to perform bilinear interpolation, and these inaccuracies result from a bug in the transform definition plus a second bug that would be fixed by#30184. However, just fixing these two bugs is not a good solution. The Agg resampler – which is intended for producing good-enough images for visual display – is problematic because it divides each pixel into 256 subpixels, which means it internally calculates 256 * M - 1 intermediate color candidates between the end colors, but the color table wants 254 intermediate colors between the end colors (to get 256 colors in total). That means fixing the aforementioned two bugs results in a non-smooth color table because 254 does not divide into 256 * M - 1 cleanly.

So, this PR instead takes the approach of eschewing the Agg resampler entirely and performing the bilinear interpolation using straight NumPy. Now the extreme colors are exactly what they should be.I'll note that the tests still need to use a bit ofatol because the definition ofBiOrangeBlue uses input that is rounded to 3 decimal places. Update: now fixed.

>>>frommatplotlib.cmimportbivar_cmaps>>>cmap=bivar_cmaps['BiOrangeBlue']>>>print(cmap.patch[0,0, :])[0.0.0.]>>>print(cmap.patch[-1,0, :])[1.0.50. ]>>>print(cmap.patch[-1,-1, :])[1.1.1.]>>>print(cmap.lut[0,0, :])[0.0.0.1.]>>>print(cmap.lut[-1,0, :])[1.0.50.1. ]>>>print(cmap.lut[-1,-1, :])[1.1.1.1.]

PR checklist

@ayshihayshihforce-pushed thesegmentedbivarcolormap_fix branch from9f1bc1e to9b4ec6dCompareDecember 8, 2025 20:03
@ayshihayshih marked this pull request as ready for reviewDecember 8, 2025 20:24
@ayshih
Copy link
ContributorAuthor

As a sanity check of the 1D analogue,LinearSegmentedColormap, here's confirmation that "Greys" goes from pure white to pure black:

>>>importmatplotlibasmpl>>>greys=mpl.colormaps["Greys"]>>>print(greys(0))(np.float64(1.0),np.float64(1.0),np.float64(1.0),np.float64(1.0))>>>print(greys(255))(np.float64(0.0),np.float64(0.0),np.float64(0.0),np.float64(1.0))

@ayshihayshihforce-pushed thesegmentedbivarcolormap_fix branch fromb3d74fc to4c66b4eCompareDecember 9, 2025 14:45
Copy link
Contributor

@greglucasgreglucas left a comment

Choose a reason for hiding this comment

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

Overall, this looks good to me and reads nice to do the math in numpy-space rather than Agg-space.

We should probably add a short change-note entry indicating this is fixing a minor bug in the interpolation scheme as it does change some results.

@greglucas
Copy link
Contributor

cc@trygvrad for a review

@ayshih
Copy link
ContributorAuthor

I realized that because theBiOrangeBlue definition using a 9x9 color array is exactly linear in all directions – once the rounding to three decimal places was removed – its definition can be drastically simplified to just a 2x2 color array.

We should probably add a short change-note entry indicating this is fixing a minor bug in the interpolation scheme as it does change some results.

I've added an entry, but let me know if it's in the wrong place

@greglucas
Copy link
Contributor

I think it should be innext_api_changes similar to this changelog entry describing an update:https://matplotlib.org/stable/api/prev_api_changes/api_changes_3.10.0.html#svg-output-improved-reproducibility

@trygvrad
Copy link
Contributor

The code looks reasonable to me, it is good to get this fixed.

@ayshih If you are actively using bivariate colormaps, I would be interested it knowing more about your use case. We will need to add more bivariate colormaps to matplotlib as theproject develops and in that context it is useful to hear from users.

@ayshihayshihforce-pushed thesegmentedbivarcolormap_fix branch from6baabe1 to5a56a90CompareDecember 11, 2025 13:57
@ayshih
Copy link
ContributorAuthor

I think it should be innext_api_changes

Ah, now I understand the system. Moved.

@ayshih If you are actively using bivariate colormaps, I would be interested it knowing more about your use case.

Heh, the bivariate colormaps look neat, but I haven't actually used them for anything. This PR came to be because I was befuddled why my fixes to the Agg resampler in#30184 affected colormaps.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@greglucasgreglucasgreglucas approved these changes

@trygvradtrygvradAwaiting requested review from trygvrad

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@ayshih@greglucas@trygvrad

[8]ページ先頭

©2009-2025 Movatter.jp