-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Issues with colormap documentation and behavior #6080
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
Comments
I'm not sure what you mean about 3D images. If you know it's greyscale, just pass one channel to the colour mapping. Matplotlib can't know this is what you want. |
Yes, this has been discussed elsewhere as well - we don't want to guess if the image is grayscale or not. I'll close... |
Um, but you do, here: matplotlib/lib/matplotlib/cm.py Lines 322 to 335 in ae2a3b1
If I use |
That is correct. We don't accept 3-d arrays unless the third dimension has length 3 or 4, in which case we interpret as rgb(a). If we accepted other 3D arrays how would we know if a (100,100,3) array was rgb or three (100,100) greyscale arrays? |
I just think the code is in a weird place where 1D, 2D, 4D, 5D, and 27D arrays work fine, but 3D arrays are special-cased in case the user happens to have passed in a 2D RGB(A) array. To enable full nD support, you could add a But anyway, this issue is from a long time ago, and I can see that there's backwards compatibility concerns, so 🤷. I wouldn't be using it in the near term, it's just the special casing that bugs me, as a frequent user of 3D grayscale images. =) |
I see, you are correct that higher dim arrays are not explicitly forbidden in the code. If it would be useful to folks, I don't see a reason to not skip the check if This is really just a one-liner at this point though: |
Over at scikit-image we were trying to write a function that would transform a grayscale image into a colormapped image of shape
im.shape + (4,)
, when @tacaswell pointed out thatcmap(image)
does this already, though this doesn't appear in the MPL docs. It turns out that this is a flaw in sphinx/numpydoc:__call__
is ignored. The method itself is in fact very well documented:https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/colors.py#L525
So that's the first issue:
The second issue is that we would probably want to use the normalizing version of this,
ScalarMappable.to_rgba
:https://github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/cm.py#L211
However, it's actually broken for nD cases, because a 3D image will unnecessarily cause a ValueError (here). So, the second (perhaps optional) issue:
ScalarMappable.to_rgba
behavior so that 3D grayscale images (numpy arrays) are supported.Happy to submit PRs for this but I'd like for some core devs to chime in regarding how best to deal with these... (In particular, I have zero experience with sphinx configuration.)
The text was updated successfully, but these errors were encountered: