matplotlib - fill_between 没有填写正确的部分
问题描述
我为可见光范围创建了具有光谱颜色的普朗克曲线。
问题:我想在曲线下创建一个阴影区域。但我未能为曲线下的正确区域着色。非常感谢。
我试过了:
1.
plt.fill_between(wavelengths,0,spectrum, color='w')
,但这不起作用。2.我也试过
plt.fill_between(wavelengths,1e13-spectrum, color='w')
了,还是不行
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors
h = 6.626e-34
c = 3.0e+8
k = 1.38e-23
def planck(wav, T):
a = 2.0*h*c**2
wav = wav*1e-9
b = h*c/(wav*k*T)
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
return intensity
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 = 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(100, 4000, 1000)
spectrum = planck(wavelengths, 5778)
plt.plot(wavelengths, spectrum, color='darkred')
y = np.linspace(0, 3e13, 10)
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.xticks(fontsize =15)
plt.xlabel('Wavelength (nm)',fontsize =20)
plt.title('Shortwave radiation',fontsize =20)
plt.text(1000,2.5e13, 'Sun 5778K',fontsize =20)
plt.fill_between(wavelengths,0,spectrum, color='w')
plt.savefig('WavelengthColors.png', dpi=200)
ax = plt.gca()
#ax.axes.get_yaxis().set_visible(False)
right_side = ax.spines["right"]
right_side.set_visible(False)
left_side = ax.spines["left"]
left_side.set_visible(False)
top_side = ax.spines["top"]
top_side.set_visible(False)
plt.ylabel("Normalized spectral emittance",fontsize =20)
plt.show()
解决方案
推荐阅读
- constraints - 从 OptaPlanner 7.46.0 切换到 8.0.0 时,约束流中断
- r - 模糊连接错误:小标题中的所有列都必须是向量
- javascript - 这个异步等待代码(javascript)有什么问题?
- laravel - 更好的开源地图,比如 laravel 的谷歌地图?
- android - 在 React Native 上无延迟地渲染许多图表
- python - 在每个端口上停止 Python localhost 服务器
- javascript - DOM 事件 attributeModified/attributeRemoved/characterDataModified... 未触发
- javascript - html5音频播放器通过innerhtml问题刷新了preventdefault
- python - 一种更有效的方式来处理我的数据并节省内存
- json - 如何从 Google 电子表格中获取日期和时间字符串作为字符串?