Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Introducing Spectral Colors#29517
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Introducing Spectral Colors#29517
Uh oh!
There was an error while loading.Please reload this page.
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join uson gitter for real-time discussion.
For details on testing, writing docs, and our review process, please seethe developer guide
We strive to be a welcoming and open project. Please follow ourCode of Conduct.
I believe the 1 failed check of the AppVeyor build is not caused by my code.
[...]
|
Yes the CI error is a known issue that happens irregularly. I’m 50/50 on the addition. Con:
Unclear: Pro: |
Colormap: i think its not supposed to be a colormap, as the full spectrum would not be the usual use case. One would rather only plot specific emission lines (one by one), where i personally would prefer them as single colors. Perhaps additionally a colormap would be nice too, yet i think to have them singular has its application. Unclear:
Same Values: it starts with this X,Y,Z values fromTable A.21 That is transformed via the transformation matrixM^-1 to linear RGB which is thengamma corrected for sRGB Alternativesif i would leave away the gamma correction, it doesn't look better (violet is missing) If i clip 0 but normalize each curve (yellow is missing): If i clip 0 and normalize to a common maximum (yellow still missing): which made me conclude that the implementation i supplied is the most accurate, despite featuring many same values in e.g. the green spectrum, but perhaps that's related to our eyes and sRGB. |
I meant a python function such as The downside is that for a single hard-coded usage The function can still use a lookup table internally. |
Ah i understand. I see the benefit of the parsing and error handling, however personally I prefer the simplicity of I think to leave out gamma correction (even as an optional feature) doesn't make sense, I just wanted to show you with that how the identical values came about and that i believe it is still the most accurate result. |
First, this is almost certainly a colormap and not individual colors. Wavelengths are not quantized integers. I don't think there is much justification for this to be named colors. Second we already have two spectral colormaps. Is this substantively different than those? A lookup function isn't a bad idea but I'm not sure it's a Matplotlib feature |
I think it really depends on the use case. Since there's a direct mapping from value to color, such a colormap would primarily make sense when used with a norm of |
jklymak commentedJan 25, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Agreed— just as there's nothing wrong with calling a color "green," there's nothing inherently wrong with using "632nm." However, very few people intuitively know what color 632nm represents, making it unhelpful for users selecting colors. The real use case is programmatically specifying the color as |
Of course nature is a continuum, but that doesn't change anything about it being useful to be able to address a wavelengths color specifically. I don't know how you imagine that, but i don't see how i can pull a wavelength color from a color map. The use here is coming from the mapping between nm and color.
i assume you are referring to 'Spectral' and 'nipy_spectral'. I think its obvious to see that 'Spectral' is just a color fade, pretty far away from physics.
This is meant for scientific work, where you sometimes explicitly use wavelength definitions as I have mentioned before (e.g. in every optics related field). Sure this wont be any useful for the ordinary user, this is a scientific contribution for scientific use-cases. |
My point still stands that this is going to be accessed programatically, in which case they are better expressed as a colormap or a function rather than hundreds of colors named with integers. I wouldn't object to a colormap that follows a well-documented standard wavelength->RGBA mapping. |
If it was a colormap, I believe the emission lines examples from#29517 (comment) could be done more efficiently withvlines. Since |
I think thats correct.
If the colormap already existed, I think you would search and find:https://matplotlib.org/stable/gallery/color/individual_colors_from_cmap.html. Of course if this colormap goes in, it may benefit from an additional example that specifies what the minimum and maximum wavelengths are so the correct norm can be used. Are there other visualization packages that refer to colors via an index into the visual wavelengths?
@rcomer that is a cool idea, though I don't see that Overall I can think of lots of cases where mapping to the visual wavelengths might make sense, and only pretty specialized cases where a named line color makes sense (eg you know a particular wavelength you want to color). My opinion is that this would be less controversial as a colormap. |
timhoffm commentedJan 26, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
You can do
Well technically it has because it produces a collection. There's only no parameter to feed a quantity to be mapped in. This should work (untested because no pc around)
|
rcomer commentedJan 26, 2025 • edited by timhoffm
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by timhoffm
Uh oh!
There was an error while loading.Please reload this page.
Here is a mockup with v3.9.2 importmatplotlib.pyplotaspltfrommatplotlib.colorsimportNormalizeimportnumpyasnprng=np.random.default_rng(42)wavelengths=rng.uniform(200,900,60)strengths=rng.random(60)fig, (ax1,ax2)=plt.subplots(nrows=2)cmap=plt.get_cmap('Spectral').with_extremes(under='black',over='black')norm=Normalize(400,800)ax1.vlines(wavelengths,0,1,cmap=cmap,norm=norm,array=wavelengths,alpha=strengths)ax2.vlines(wavelengths,0,strengths,cmap=cmap,norm=norm,array=wavelengths)plt.show() |
scottshambaugh commentedJan 28, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
My opinions here:
To tag on to the example, getting and using the RGBa value of a wavelength once the colormap is defined is pretty simple: importmatplotlib.pyplotaspltfrommatplotlib.colorsimportNormalizeimportnumpyasnpcmap=plt.get_cmap('nipy_spectral').with_extremes(under='black',over='black')norm=Normalize(400,800)# Getting the RGBa value for a wavelengthc750nm=cmap(norm(750))print(c750nm)# (0.9242019607843137, 0.0, 0.0, 1.0)# Using that color in a plotx=np.arange(0,1,0.01)plt.plot(x,np.sin(x),c=c750nm)plt.show()# red line |
Name suggestion for the colormap: "spectrum390-780". IMHO including the limits in the name is essential. |
The mappings seem to come from someone’s PhD dissertation. Do we need to get permission from someone to use them? |
No, to use published results does not need permission of the author, it does need a citation. In this case the result is a spectral function standard named after her institution 2006-TUIL-2° which is meant to be an improved version of the commonCIE color space - the whole point of that PhD was to create a mapping that is supposed to be used. I am taking away from this discussion that an implementation as colormap is preferred, which i also come to agree with now. I will implement it as such, however I am not sure when i have time to do it. |
It would also be preferable if this mapping were recognized beyond a PhD thesis. Is this an actual standard in wide use? |
PR summary
Why: Scientific plots as e.g.spectral line plots, could not directly access colors through wavelengths.
Solution: Define colors through wavelength strings e.g. '405nm' that get mapped to RGB values
Method: Precomputed RGB values. XYZ values from the2006-TUIL-2° spectral value function converted to sRGB with gamma correction.
This code:
yields:
PR checklist