Thanks to visit codestin.com
Credit goes to github.com

Skip to content

colormaps don't have getters for under, over, bad colors. #16075

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

Closed
pharshalp opened this issue Jan 2, 2020 · 6 comments
Closed

colormaps don't have getters for under, over, bad colors. #16075

pharshalp opened this issue Jan 2, 2020 · 6 comments

Comments

@pharshalp
Copy link
Contributor

pharshalp commented Jan 2, 2020

Bug report

Bug summary
colormaps don't have getters for under, over, bad colors (although the corresponding setters exist).
I am writing a function that accepts a colormap as an argument. I want to ensure that the colormap passed to the function has the same color for the "bad" and "under" conditions. The only way around this is to access "private" attributes cmap._rgba_under, cmap._rgba_bad as shown below.

Code for reproduction

cmap = plt.get_cmap('cividis')
cmap.set_bad('k')
cmap.set_under('k')

assert cmap._rgba_bad == cmap._rgba_under

If adding getters (for under, over, bad colors) for the colormaps sounds like a good improvement, i would be interested in submitting a PR.

Actual outcome

# If applicable, paste the console output here
#
#

Expected outcome

Matplotlib version

  • Operating system:
  • Matplotlib version:
  • Matplotlib backend (print(matplotlib.get_backend())):
  • Python version:
  • Jupyter version (if applicable):
  • Other libraries:
@anntzer
Copy link
Contributor

anntzer commented Jan 2, 2020

Actually I think the setters should not exist (see #14645)...
I think you can do cmap(+np.inf) cmap(-np.inf) cmap(np.nan) to get the over/under/bad values, not sure if it warrants a separate API?

@pharshalp
Copy link
Contributor Author

Thanks.

@MaozGelbart
Copy link
Contributor

@anntzer perhaps it warrants a different API? The difference is distinguished when bad/under/over were not set at all. Querying the cmap for such cases will result with the default extreme colors even when these were never set by the cmap creator. With getters I expect to get the set value (if provided) or None (if these weren't set at all).

@jklymak
Copy link
Member

jklymak commented Jan 28, 2020

Not sure I follow. By default you definitely want a color if the mapped value is over vmax.

@MaozGelbart
Copy link
Contributor

For sure, but currently I can't tell apart if set_over was called prior to that or I'm getting the default extreme value from the colormap. So by having a get_over that returns None if set_over was never called (or the input to set_over in the case it was called), I can distinguish between the two cases. This may prove useful for manipulating colormaps.

@pharshalp
Copy link
Contributor Author

pharshalp commented Jan 28, 2020

@MaozGelbart

import matplotlib.pyplot as plt
import numpy as np

cmap = plt.get_cmap('cividis')
# the following will return True since set_under was not used to set "under" color
np.allclose(cmap(0), cmap(-np.inf))  # True

cmap.set_under('w')
np.allclose(cmap(0), cmap(-np.inf))  # False

# similarly for set_over ...

np.allclose(cmap(255), cmap(np.inf))  # True
cmap.set_under('k')
np.allclose(cmap(255), cmap(np.inf))  # False

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

No branches or pull requests

4 participants