【问题标题】:Graphing the combinations of 2 out of phase Planck functions using python使用python绘制2个异相普朗克函数的组合
【发布时间】:2017-06-27 19:13:48
【问题描述】:

我试图绘制 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()

【问题讨论】:

  • 您想将两个绘图函数相加吗?
  • 是的。我不知道该怎么做。

标签: python python-3.x graphing


【解决方案1】:

解决此问题的一种方法是在移动数据的开头(使用np.concatenate((np.zeros(shift),intensity)))填充0,并在对两个数据集求和之前在末尾删除相同数量的数据(切片[:-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()

可能有一种更简单的方法,但这是一种相当直接的方法。

【讨论】:

  • 非常感谢。这在正确转换任何功能时应该有效吗?如果我必须垂直移动它怎么办? [:-shift] 还有什么作用?
  • 适用于任何功能。我可以看看我是否花时间将过程隔离在一个函数中。如果要垂直移动,只需在强度数组中添加一个常数即可。 [:-shift] 是数组的一个切片,从开头开始直到结束前的移位单位。
  • 好吧,这是有道理的。如果我想找到每条曲线下的面积呢?我尝试使用 quad 函数,但它一直给我一个错误,说它不可调用。抱歉,我对这一切还是陌生的,我确信这似乎是一个简单的问题。
  • 我想通了,谢谢,你帮了大忙。
猜你喜欢
  • 1970-01-01
  • 2020-05-31
  • 1970-01-01
  • 2017-03-10
  • 2021-12-28
  • 2014-04-20
  • 1970-01-01
  • 2013-01-16
  • 2011-07-15
相关资源
最近更新 更多