使用 python 绘制 2 个异相普朗克函数的组合图
Graphing the combinations of 2 out of phase Planck functions using python
我正在尝试绘制 2 个图形的总和。当它是一个数字数组或一条直线时,我通过组合 y 值轻松地做到了这一点,但现在我使用的是同一个图形,只是稍微移动了一点,y 值是由一个函数确定的,所以我不确定该怎么做。这是我到目前为止所做的,我的一些尝试也是#'d。
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
#wave=np.arange((1+500)*10**-9,3e-6,1e-9)
intensity6000= planck( wavelengths, 6000.)
#intensity6001=planck(wavelengths+(500*10**-9),6000)
#sum_of_values=intensity6000+
plt.plot(wavelengths*1e9, intensity6000, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9+500, intensity6000, 'b', label='6000k shifted')
#plt.plot(wavelengths*1e9+wavelengths*1e9+500, intensity6000) this is wrong
it shifts it again doesnt show the total
#plt.plot(wavelengths*1e9+500,intensity6001) #plots a straight line at 0
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
解决此问题的一种方法是在移位数据的开头填充 0(使用 np.concatenate((np.zeros(shift),intensity))
)并在末尾删除相同数量的数据(切片 [:-shift]
)在对两个数据集求和之前。
您的代码的清理版本如下
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
shift = 500 # defining the shift
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
intensity = planck( wavelengths, 6000.)
# creating a shifted intensity by padding 0 in the begining
# and removing extra values at the end
shifted_intensity = np.concatenate((np.zeros(shift),intensity))[:-shift]
sum_intensity = intensity + shifted_intensity
plt.plot(wavelengths*1e9, intensity, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9, shifted_intensity, 'b-', label='6000 K shifted')
plt.plot(wavelengths*1e9, sum_intensity, 'r-', label= 'Summed intensity')
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
可能有更简单的方法,但这是一种相当直接的方法。
我正在尝试绘制 2 个图形的总和。当它是一个数字数组或一条直线时,我通过组合 y 值轻松地做到了这一点,但现在我使用的是同一个图形,只是稍微移动了一点,y 值是由一个函数确定的,所以我不确定该怎么做。这是我到目前为止所做的,我的一些尝试也是#'d。
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
#wave=np.arange((1+500)*10**-9,3e-6,1e-9)
intensity6000= planck( wavelengths, 6000.)
#intensity6001=planck(wavelengths+(500*10**-9),6000)
#sum_of_values=intensity6000+
plt.plot(wavelengths*1e9, intensity6000, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9+500, intensity6000, 'b', label='6000k shifted')
#plt.plot(wavelengths*1e9+wavelengths*1e9+500, intensity6000) this is wrong
it shifts it again doesnt show the total
#plt.plot(wavelengths*1e9+500,intensity6001) #plots a straight line at 0
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
解决此问题的一种方法是在移位数据的开头填充 0(使用 np.concatenate((np.zeros(shift),intensity))
)并在末尾删除相同数量的数据(切片 [:-shift]
)在对两个数据集求和之前。
您的代码的清理版本如下
import matplotlib.pyplot as plt
import numpy as np
h=6.626e-34
c= 3.0e+8
k= 1.38e-23
shift = 500 # defining the shift
#wav is the wavelength
def planck(wav, T):
a= 2.0*h*c**2
b= (h*c)/(wav*k*T)
intensity = a/( (wav**5) * (np.exp(b)- 1.0) )
return intensity
wavelengths= np.arange(1e-9, 3e-6, 1e-9)
intensity = planck( wavelengths, 6000.)
# creating a shifted intensity by padding 0 in the begining
# and removing extra values at the end
shifted_intensity = np.concatenate((np.zeros(shift),intensity))[:-shift]
sum_intensity = intensity + shifted_intensity
plt.plot(wavelengths*1e9, intensity, 'm-', label= '6000 K')
plt.plot(wavelengths*1e9, shifted_intensity, 'b-', label='6000 K shifted')
plt.plot(wavelengths*1e9, sum_intensity, 'r-', label= 'Summed intensity')
plt.xlabel('Wavelength\n(nm)')
plt.ylabel('Intensity\n(W Sr^-1 m^-3)')
plt.title('Black Body Radiation')
plt.legend()
plt.show()
可能有更简单的方法,但这是一种相当直接的方法。