首先,您需要一个将波长作为输入并返回RGB颜色的函数.可以找到一个这样的函数here.可以对其进行调整,使其还返回一个alpha值,该值在可见颜色范围之外小于1.
此功能可用于创建颜色图.使用体面的归一化可以将波长范围映射到0到1之间的范围,以便可以在imshow图中使用此色图.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
def wavelength_to_rgb(wavelength, gamma=0.8):
''' taken from http://www.noah.org/wiki/Wavelength_to_RGB_in_Python
This converts a given wavelength of light to an
approximate RGB color value. The wavelength must be given
in nanometers in the range from 380 nm through 750 nm
(789 THz through 400 THz).
Based on code by Dan Bruton
http://www.physics.sfasu.edu/astro/color/spectra.html
Additionally alpha value set to 0.5 outside range
'''
wavelength = float(wavelength)
if wavelength >= 380 and wavelength <= 750:<