Skip to content
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

Fix get_cmap MatplotlibDeprecationWarning #633

Merged
merged 2 commits into from
Apr 17, 2023
Merged

Fix get_cmap MatplotlibDeprecationWarning #633

merged 2 commits into from
Apr 17, 2023

Conversation

nicovank
Copy link
Contributor

[~] ~/hyperfine/scripts/plot_whisker.py -o result.jpg results.json
~/hyperfine/scripts/plot_whisker.py:37: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed two minor releases later. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap(obj)`` instead.
  cmap = plt.cm.get_cmap("rainbow")

After this change, the warning disappears, and the output plot looks unchanged.

@sharkdp
Copy link
Owner

sharkdp commented Apr 2, 2023

Thank you. Does this put a new upper limit on the used matplotlib version?

@nicovank
Copy link
Contributor Author

nicovank commented Apr 3, 2023

Matplotlib 3.7 was just released, but it looks like this was actually deprecated since 3.6.

@sharkdp
Copy link
Owner

sharkdp commented Apr 3, 2023

Sorry, I meant: lower limit. In the sense of: this will probably not work on (very) old versions of matplotlib.

@vadcx
Copy link

vadcx commented Apr 16, 2023

@sharkdp Yes, looks like the new method bumps the minimum version to 3.5:

In Matplotlib 3.5 we added a .ColormapRegistry class and exposed an instance at the top level as matplotlib.colormaps. The existing top level functions in matplotlib.cm (get_cmap, register_cmap, unregister_cmap) were changed to be aliases around the same instance. In Matplotlib 3.6 we have marked those top level functions as pending deprecation. source 3.7.0

Colormaps are now managed via matplotlib.colormaps (or .pyplot.colormaps), which is a .ColormapRegistry. While we are confident that the API is final, we formally mark it as experimental for 3.5
source 3.5.0

For 3.9 (or later): Remove all the functions we want to remove. matplotlib/matplotlib#20853

@nicovank
Copy link
Contributor Author

Correct. I found that out last week but didn’t have time to update this PR. I think this can stay as-is at least until get_cmap is officially removed. Maybe there’s a way to remove usage entirely, I’ll try to take another look today.

@vadcx
Copy link

vadcx commented Apr 16, 2023

@nicovank Literally, why not both? The simplest imho is to dynamically choose either of them and call the existing variant (as long the underlying API is the same? I didn't check). That's gonna be the fastest solution?

If you want to recreate the rainbow colors it's as simple as iterating over the HSL color space: different degree 0-360° on the spectrum with maximum saturation and luminance.

If you want to exercise in the above, there's the human-friendlier version https://www.hsluv.org/ :) The HSL iteration is a couple of lines of code in any language, HSLuv is going to be a required dependency for a minimal effect.

@nicovank
Copy link
Contributor Author

As per matplotlib/matplotlib#20853:

Keep plt.get_cmap() - This is too widely used to be removed.

So I think it's best here to just use that (versus plt.cm.get_cmap).

AFAIK this does not set a new lower bound to the used Matplotlib. I tested on my machine down to 2.0.0 (released January 2017). Beyond this, it fails for some unrelated (tkinter) reason.

@sharkdp
Copy link
Owner

sharkdp commented Apr 17, 2023

I tested on my machine down to 2.0.0 (released January 2017).

Okay. So the following statement was incorrect?

looks like the new method bumps the minimum version to 3.5:

@nicovank
Copy link
Contributor Author

It was accurate with the previous change (see commit history). plt.get_cmap (current version) seems to have been supported for a long time.

@sharkdp
Copy link
Owner

sharkdp commented Apr 17, 2023

It was accurate with the previous change (see commit history). plt.get_cmap (current version) seems to have been supported for a long time.

Oh, sorry. I missed the new commit.

Thank you!

@sharkdp sharkdp merged commit 7684efd into sharkdp:master Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants