python - 绘制理想化黑体光谱
问题描述
所以我遇到了几个错误
RuntimeWarning: overflow encountered in exp
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
和
RuntimeWarning: invalid value encountered in multiply
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
即使有这些错误(还有一个关于除以零的错误,我忽略了哈哈),我的图表也能以任何一种方式正确生成。我只是想知道是否有人可以帮助我清除这些错误?请和谢谢。
这是完整的代码:
import numpy as np
import matplotlib.pyplot as plt
from astropy import constants as const
def planck(T, wav):
a = 2.0*h*c**2
b = h*c/(wav*k*T)
intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
return intensity
# Part 1: Plotting Planck's Law
T1 = 3750
T2 = 5200
T3 = 9600 # Temperature of M0 star, the Sun, and A0 star (K)
c = const.c.value
h = const.h.value
k = const.k_B.value
l = np.linspace(0, 1.5e-6, 1500) #Array of wavlengths (meters)
IM0 = planck(T1, l)
Isun = planck(T2, l)
IA0 = planck(T3, l) # Planck's law intensities
plt.figure(1) # Plot of the three idealized blackbody spectra
plt.plot(l, IM0, 'k-', label = 'M0 Star')
plt.plot(l, Isun, 'r--', label = 'Sun')
plt.plot(l, IA0, 'b-.', label = 'B0')
plt.xlabel('Wavelength (meters)')
plt.ylabel('Intensity (W sr^{-1} m^{-3})')
plt.title('Idealized Blackbody Spectra')
#plt.legend('M0 Star', 'Sun', 'B0 Star')
leg = plt.legend()
plt.ticklabel_format(axis="x", style="sci", scilimits=(0,0)) # Scientific not
解决方案
的前 5 个值l
太小,这会导致 的值很高,从而导致b
数值溢出exp
(因为 exp(1500) 只是一个非常大的数字)。
事实上,in 的第一个值l
只是零,因此wav
inplanck()
变得无限并且1/wav**5
是 NaN。
因此所有的警告。设置l = np.linspace(6e-9, 1.5e-6, 1500)
,你会没事的。
不,index,这不是 IDE 警告,而是 Python 警告。有一些方法可以抑制此类警告,但只有在您确切知道要抑制什么以及为什么要抑制时才能这样做。
推荐阅读
- parsing - 用Java从字符串中打印IP地址和子网掩码
- python - 在文本框中排列文本
- reactjs - React-Native 动画视图进出
- javascript - 为什么节点文档在描述这个函数时会在逗号之前列出这个左括号?
- python-3.x - 支持的目标类型是:('binary', 'multiclass')。改为“连续”
- bash - 在 bash 中,我可以将进度表及其格式保存到 cURL 请求的变量中吗?
- php - 邮递员:以数字形式查询正文值
- python - scipy 最小化在优化期间不探索所有样本空间
- c++ - c++ g++ 编译器 - 错误:隐式声明的定义
- javascript - 如何在对象数组中获取特定的键和值