1

I have two ".txt" files, wavelength.txt & spectrum.txt (containing intensity for each wavelength). Now I want to plot intensity vs wavelength, where the curved in the visible range of wavelength should be shaded according to visible range spectra color. Something like this attached picture enter image description here I have tried this with one already reported post: (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()

But I am getting an error like this

ValueError: invalid literal for float(): 

Here are the part of the data of two files :

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
MBT
  • 21,733
  • 19
  • 84
  • 102
L_Li_25
  • 11
  • 2
  • 1
    You have an issue in reading the float data from the file, use this `spectrum = [float(x) for x in spectru]` for spectrum and do similar thing for the wavelength file. – hesham_EE Sep 06 '18 at 17:25
  • You would probably want to use `numpy.loadtxt` or `numpy.genfromtxt` to read in your data. – ImportanceOfBeingErnest Sep 06 '18 at 18:23
  • @ImportanceOfBeingErnest Thanks for your prompt response. It almost done. But the color shade is crossing the basic (original) plot height in the Y-axis. Means the color shade is not under the curve (plot). Here is the image https://drive.google.com/open?id=1Op0nA0VRcZV4_-Xol1YRcaOhMuAwH027 – L_Li_25 Sep 06 '18 at 21:18
  • It's hard to know what you mean and one cannot test because the code is not runnable. For such problems you would rather create a [mcve], i.e. create some mock data in the code that reproduce the issue. – ImportanceOfBeingErnest Sep 06 '18 at 21:22
  • @ImportanceOfBeingErnest here is the data that I am testing, spectrum : https://drive.google.com/file/d/1qNm0VkUOWSM__1RShWOtUN97wGt0SSAy/view?usp=sharing and wavelength : https://drive.google.com/file/d/1E7Pmini5X-9mWfOMGcTzbj4vxl6XmPNk/view?usp=sharing – L_Li_25 Sep 06 '18 at 21:42
  • @ImportanceOfBeingErnest I have also noticed the shift of color shade. Instead of 350 to 780 in X-axis, it is now 460 to 825. Here is the image generated with those data https://drive.google.com/open?id=1Op0nA0VRcZV4_-Xol1YRcaOhMuAwH027 – L_Li_25 Sep 07 '18 at 08:29

0 Answers0