Skip to content

FIX: Checks for (value, color) tuples in LinearSegmentedColormap.from_list #29052

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 1 commit into from
Feb 1, 2025

Conversation

3j14
Copy link
Contributor

@3j14 3j14 commented Oct 31, 2024

PR summary

Fixes #29042.

By default, a list of colors is assumed. Only if the conversion to rgba values fails, check for (value, color) pairs.

The check of the first entry in colors in preserved to raise more meaningful error messages.

For the first item to be interpreted as a valid (value, color) tuple, it has to be

  • of type Sized (implements __len__),
  • not of type str,
  • length 2.
  • first entry being a real number
  • second entry passing the matplotlib.colors.is_color_like test

Either way, errors are returned if not all entries are (value, color) pairs or if any invalid colors are encountered.

Implemented tests for different combinations of inputs.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@3j14
Copy link
Contributor Author

3j14 commented Oct 31, 2024

I must say, the code got a whole lot more complicated. If you think it became too unreadable, I'd be happy to discuss how we can improve it. But I felt like these checks were important to catch user errors and give meaningful feedback.

The problem is that the inputs are ambiguous, and it is not possible to tell what the user intends to do. For now this is solved by just looking at the first entry.

@tacaswell tacaswell added this to the v3.11.0 milestone Nov 1, 2024
@3j14 3j14 force-pushed the fix-colors-from_list branch from eb8a638 to bb550e1 Compare November 1, 2024 21:52
@3j14
Copy link
Contributor Author

3j14 commented Nov 8, 2024

Concerning the pull request: Do you prefer if all commits are squashed into one, or will you take care of this when merging?

@3j14
Copy link
Contributor Author

3j14 commented Nov 8, 2024

Not sure how the failed checks are related to my changes. Are those just flaky checks?

@timhoffm
Copy link
Member

timhoffm commented Dec 6, 2024

Sorry for the late reply.

Do you prefer if all commits are squashed into one, or will you take care of this when merging?

You are welcome to squash. If not, we'll do it on merge.

Not sure how the failed checks are related to my changes. Are those just flaky checks?

The CI runs have been deleted meanwhile. I assume the failures were unrelated. We had a couple of failing tests recently. Please try to rebase, that should likely fix it.

@3j14 3j14 force-pushed the fix-colors-from_list branch 2 times, most recently from 15a693c to 0fae9bd Compare December 9, 2024 10:16
@3j14
Copy link
Contributor Author

3j14 commented Dec 9, 2024

Thanks for your feedback! I added your remarks, squashed the commits and rebased on main.
mypy failing appears to be unrelated.

@3j14 3j14 force-pushed the fix-colors-from_list branch from 0fae9bd to eb02204 Compare December 9, 2024 15:03
@3j14
Copy link
Contributor Author

3j14 commented Dec 9, 2024

Thanks! Had to use assert_array_almost_equal for all checks – I assume there are some slight rounding errors caused by floating-point arithmetic. Similar tests also use the assert_array_almost_equal which was already imported.

Assume that the 'colors' argument is always a list of colors.
If 'to_rgba_array' fails, attempt to unpack the values as a list of
'(color, value)' tuples.

Fixes matplotlib#29042. Passing '(color, alpha)' tuples are nor correctly parsed
as a list of colors.

Check for range and monotony of values when unpacking (value, color)
pairs.

Implemented tests for different combinations of inputs.
@3j14 3j14 force-pushed the fix-colors-from_list branch from eb02204 to d9b8020 Compare December 9, 2024 17:44
@3j14
Copy link
Contributor Author

3j14 commented Dec 9, 2024

The last commit had some old unused parameter in one test case, causing pytest to complain about a missing fixture.

Sorry for the confusion. I amended that commit already but forgot to push 😅

@3j14 3j14 requested a review from tacaswell January 24, 2025 12:59
@tacaswell tacaswell closed this Jan 31, 2025
@tacaswell tacaswell reopened this Jan 31, 2025
@tacaswell
Copy link
Member

"power cycled" to get CI to re-run against current main (which should fix the mypy failures).

@timhoffm timhoffm merged commit 46ea8f0 into matplotlib:main Feb 1, 2025
63 of 65 checks passed
@timhoffm
Copy link
Member

timhoffm commented Feb 1, 2025

Thanks @3j14 and congratulations on your first contribution to Matplotlib! 🎉

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

Successfully merging this pull request may close these issues.

[Bug]: colors.LinearSegmentedColormap.from_list fails when using a ("<color>", alpha) tuple
3 participants