Matplotlib颜色图基于可见光范围波长

时间:2018-09-06 16:59:45

标签: python matplotlib

我有两个“ .txt”文件,length.txt和Spectrum.txt(包含每个波长的强度)。现在,我想绘制强度与波长的关系图,其中应根据可见范围光谱的颜色对可见波长范围内的曲线进行阴影处理。这张图片enter image description here之类的东西 我已经在一篇已经报道的帖子中进行了尝试:(Matplotlib - color under curve based on spectral color

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:
    A = 1.
else:
    A=0.5
if wavelength < 380:
    wavelength = 380.
if wavelength >750:
    wavelength = 750.
if wavelength >= 380 and wavelength <= 440:
    attenuation = 0.3 + 0.7 * (wavelength - 380) / (440 - 380)
    R = ((-(wavelength - 440) / (440 - 380)) * attenuation) ** gamma
    G = 0.0
    B = (1.0 * attenuation) ** gamma
elif wavelength >= 440 and wavelength <= 490:
    R = 0.0
    G = ((wavelength - 440) / (490 - 440)) ** gamma
    B = 1.0
elif wavelength >= 490 and wavelength <= 510:
    R = 0.0
    G = 1.0
    B = (-(wavelength - 510) / (510 - 490)) ** gamma
elif wavelength >= 510 and wavelength <= 580:
    R = ((wavelength - 510) / (580 - 510)) ** gamma
    G = 1.0
    B = 0.0
elif wavelength >= 580 and wavelength <= 645:
    R = 1.0
    G = (-(wavelength - 645) / (645 - 580)) ** gamma
    B = 0.0
elif wavelength >= 645 and wavelength <= 750:
    attenuation = 0.3 + 0.7 * (750 - wavelength) / (750 - 645)
    R = (1.0 * attenuation) ** gamma
    G = 0.0
    B = 0.0
else:
    R = 0.0
    G = 0.0
    B = 0.0
return (R,G,B,A)

clim=(350,780)
norm = plt.Normalize(*clim)
wl = np.arange(clim[0],clim[1]+1,2)
colorlist = zip(norm(wl),[wavelength_to_rgb(w) for w in wl])
spectralmap = 
matplotlib.colors.LinearSegmentedColormap.from_list("spectrum", colorlist)

fig, axs = plt.subplots(1, 1, figsize=(8,4), tight_layout=True)

wavelengths = open("wave.txt", "r") 
spectru = open("spectrum.txt", "r")

spectrum = float(spectru.read())
plt.plot(wavelengths, spectrum, color='darkred')
y = np.linspace(0, 6, 100)
X,Y = np.meshgrid(wavelengths, y)
extent=(np.min(wavelengths), np.max(wavelengths), np.min(y), np.max(y))
plt.imshow(X, clim=clim,  extent=extent, cmap=spectralmap, aspect='auto')
plt.xlabel('Wavelength (nm)')
plt.ylabel('Intensity')
plt.show()

但是我遇到这样的错误

ValueError: invalid literal for float(): 

这是两个文件的数据的一部分:

spectrum-
   7
   31
   129
   925
   3381
   26600
   104730
   529542
   4065029
   13154865
   50846469


wavelength

   284
284.5
285
285.5
286
286.5
287
287.5
288
288.5
289
289.5
290
290.5

0 个答案:

没有答案