Skip to content

Use standard property alias machinery in contour(). #30433

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
Aug 24, 2025

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Aug 15, 2025

Note that 'color(s)' remains special-cased because the ContourSet constructor needs to do a fair bit of special wrangling for it.

Closes #1963.

PR summary

PR checklist

@anntzer anntzer force-pushed the ck branch 2 times, most recently from a9055a1 to 6b001dc Compare August 16, 2025 08:54
@QuLogic QuLogic added this to the v3.11.0 milestone Aug 18, 2025
Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

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

Wouldn't it be more internally consistent to let "color" be an alias for "colors" instead of treating it as an error? edgecolor and facecolor are both accepted.


def test_contour_singular_color():
with pytest.raises(TypeError):
plt.figure().add_subplot().contour([[0]], color="r")
Copy link
Member

Choose a reason for hiding this comment

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

The TypeError is coming from the wrong place, the first argument rather than the second. To fix, use
plt.figure().add_subplot().contour([[0, 1], [2, 3]], color="r").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, fixed.

Copy link
Member

Choose a reason for hiding this comment

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

Not a blocker, but it’s typically a good idea to use pytest.raises(…, match=…) to make the test more specific. This would likely have caught the issue of catching the wrong exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the issue is rather that contour([[0]]) should raise a ValueError rather than a TypeError (the input has the right type, after all).

Copy link
Member

Choose a reason for hiding this comment

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

I agree that ValueError would be more appropriate. But that doesn’t reduce the value of narrowing down tests via match.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll leave this one as it's for now, changing pytest.raises to more generally test the error message seems like a good idea but also seems a separate issue.

Copy link
Member

Choose a reason for hiding this comment

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

Ok.

Note that 'color(s)' remains special-cased because the ContourSet
constructor needs to do a fair bit of special wrangling for it.
@anntzer
Copy link
Contributor Author

anntzer commented Aug 23, 2025

I intentionally chose to not support 'color' as alias for 'colors' because 'colors' is actually not a ContourSet property (there's no ContourSet.set_colors, unlike 'edgecolors' and 'facecolors', which are aliases for the respective singular forms). Moreover, whereas ContourSet.set_color (singular) does exist, it doesn't really work -- try e.g.

from pylab import *
contourf(np.arange(100).reshape(10, 10), levels=[20, 50, 80]).set_color(["c", "m", "y", "k"])

which leaves the spanned region with the default colors and tries to set colors on the boundaries instead.

Turning 'color'/'colors' into a real ContourSet property could be a followup issue, though.

Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

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

Unrelated test failures? Otherwise, looks OK to me.

@anntzer
Copy link
Contributor Author

anntzer commented Aug 24, 2025

Seems unrelated indeed.

@timhoffm timhoffm merged commit 1f8ff33 into matplotlib:main Aug 24, 2025
38 of 40 checks passed
@anntzer anntzer deleted the ck branch August 24, 2025 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Singular keyword arguments in contour don't raise exceptions
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy