栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Matplotlib-基于光谱颜色的曲线下颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Matplotlib-基于光谱颜色的曲线下颜色

首先,您需要一个将波长作为输入并返回RGB颜色的函数。这样的功能可以在这里找到。可以使它适应返回一个Alpha值,该Alpha值在可见颜色范围之外小于1。

此功能可用于创建颜色图。使用体面的归一化可以将波长范围映射到0到1之间的范围,以便可以在imshow图中使用此色图。

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.colorsdef 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 pre 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 = list(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 = np.linspace(200, 1000, 1000)spectrum = (5 + np.sin(wavelengths*0.1)**2) * np.exp(-0.00002*(wavelengths-600)**2)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.fill_between(wavelengths, spectrum, 8, color='w')plt.savefig('WavelengthColors.png', dpi=200)plt.show()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647085.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号