8000 MNT: Re-add matplotlib.cm.get_cmap by timhoffm · Pull Request #28355 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MNT: Re-add matplotlib.cm.get_cmap #28355

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
Jun 26, 2024
Merged

Conversation

timhoffm
Copy link
Member
@timhoffm timhoffm commented Jun 6, 2024

Addresses #28349 (comment)

We are seeing multiple reports that the warnings in 3.8 were not seen. We should sort out why and consider putting the API in a micro release with a more visible warning (at maybe a custom AttributeError that gives more context?).

This was removed in 3.9, but apparently caused more user trouble than expected. Re-added for 3.9.1 and extended the deprecation period for two additional minor releases.

I did not find a technical reason why people may not see it. My wild guess is that this was quite commonly used and there are enough cases where people jump more than 2 minor versions. Hopefully extending for 2 additional minor releases will be good enough. - I question whether this was too common and should not have been removed at all, but now that we've started IMHO we should eventually remove.

@timhoffm timhoffm added this to the v3.9.1 milestone Jun 6, 2024
@timhoffm timhoffm marked this pull request as draft June 7, 2024 10:01
Copy link
Contributor
@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

I think mypy is complaining that it isn't public here.

This was showing up previously:
#28349 (comment)
So I agree it is likely people jumping more than 2 versions at a time.

This seems reasonable to extend the deprecation, but I still think it is a good idea to remove this function and push people towards what we want them to use, the colormaps registry. Users can still use plt.get_cmap instead of matplotlib.cm.get_cmap if they want a function call instead of dict lookup style access.

@anntzer
Copy link
Contributor
anntzer commented Jun 7, 2024

Just for reference, another use of get_cmap I've found in the wild: https://github.com/yt-project/yt_idv/blob/219338c989a1b67361893744385ddb3d543c8083/yt_idv/opengl_support.py#L224

I would be +0 for keeping that function around (especially as it's staying in pyplot!). Perhaps it could be discouraged in the docs, but it's also a bit weird because that'll just push people to use the pyplot version... whereas we usually don't want to push users in that direction (even though this case is of course quite different from the usual issues with pyplot).

@greglucas
Copy link
Contributor
greglucas commented Jun 7, 2024

Just for reference, another use of get_cmap I've found in the wild: https://github.com/yt-project/yt_idv/blob/219338c989a1b67361893744385ddb3d543c8083/yt_idv/opengl_support.py#L224

That is actually the get_cmap() that is attached to the registry, so wouldn't hit this warning that is off of the module-level version.
https://github.com/yt-project/yt_idv/blob/219338c989a1b67361893744385ddb3d543c8083/yt_idv/opengl_support.py#L21

edit: which makes me wonder if this function could be simplified to return _colormaps.get_cmap() instead of copy-pasting the code block?

@tacaswell
Copy link
Member

We had at least one other case (which I am completely failing to find the issue for) where downstream never saw the warnings because they were running in a configuration where they were silent.

@timhoffm
Copy link
Member Author
timhoffm commented Jun 8, 2024

edit: which makes me wonder if this function could be simplified to return _colormaps.get_cmap() instead of copy-pasting the code block?

The the old get_cmap code block (and also the pyplot one) is almost identical to

    if lut is None:
        return _colormaps.get_cmap(name)
    else:
        return _colormaps.get_cmap(name).resampled(lut)

The only difference is that the exception for unexpected types is explicitly handled in ColormapRegistry.get_cmap and thus returns TypeError, whereas the older get_cmap() returns ValueError. Likely ok to change that, but we should be at least consistent between cm and pyplot.

@anntzer
Copy link
Contributor
anntzer commented Jun 8, 2024

FWIW the version in yt_idv was explicitly checking for ValueError, but at least in this case it is trivial to write code that works in all cases (one just needs to catch both ValueError and KeyError).

@QuLogic
Copy link
Member
QuLogic commented Jun 13, 2024

Discussion on the call went the way of merging this; is there something left to move this out of draft?

@timhoffm
Copy link
Member Author
timhoffm commented Jun 13, 2024

Is there a consensus on temporarily vs permanent reactivation?

I‘m now +0 on permanent but discouraged. I suppose I may have underestimated people using cm.get_cmap (because plt.get_cmap is easier accessible. 🤷 ). But possibly still not worth breaking many users. - My priorities between a small and clean interface and backward compatibility evolve…

semi-OT: It may be worth expanding on discouraging. I.e. keeping API but steering people away. Including optionally issuing runtime warnings. This can move the usage patterns over time. -> better user code for most, no forced changes. A possible later deprecation will be less disruptive.

@tacaswell
Copy link
Member

Consensus was "temporary"

@timhoffm timhoffm marked this pull request as ready for review June 14, 2024 17:45
This was removed in 3.9, but apparently caused more user trouble than
expected. Re-added for 3.9.1 and extended the deprecation period for
two additional minor releases.
@ksunden ksunden merged commit 22b4667 into matplotlib:main Jun 26, 2024
41 of 42 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Jun 26, 2024
QuLogic added a commit that referenced this pull request Jun 26, 2024
…355-on-v3.9.x

Backport PR #28355 on branch v3.9.x (MNT: Re-add matplotlib.cm.get_cmap)
@timhoffm timhoffm deleted the get_cmap branch January 18, 2025 20:48
@dstansby dstansby mentioned this pull request May 3, 2025
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.

6 participants
0