使用 matplotlib 绘制黑体辐射曲线,但我不明白我遇到的错误?

Plotting a blackbody radiation curve using matplotlib, but I dont understand the error I'm getting?

import math
import random
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
h=6.62607015E-34 # Planck constant, units of J*s
hbar=h/(2*math.pi) # Reduced Planck constant 
k=1.380649E-23 # Boltzmann constant, units of J/K
c=299792458.00 # speed of light M/s
sb=(2*(math.pi**5)*(k**4))/(15*(c**2)*(h**3)) # Stefan-Boltzmann constant, units of W/(m^2 K^4)
def one_star():
    mass=random.uniform(0.1,50) # random mass in relation to the sun's mass
    print("The mass of the star is "+str(mass)+" times the mass of the Sun")
    if mass <= 1.4: # given by the mass-radius relationship
        radius=mass**0.9
        print("The radius of the star is "+str(radius)+" times the radius of the Sun")
    else:
        radius=mass**0.6 # larger stars are less dense
        print("The radius of the star is "+str(radius)+" times the radius of the Sun")

    # To find the volume and surface area of the star, we want values in terms of meters^3 and meters^2
    # So we'll multiply each of the previous values by the Sun's mass and radius.
    mass_actual=mass*1.989E28 # mass in kilograms
    print("The mass of the star in kilograms: "+str(mass_actual))
    radius_actual=radius*696000000 # radius in meters
    print("The radius of the star in meters: "+str(radius_actual))   
    volume=(4/3)*math.pi*(radius_actual**3) # volume in meters cubed
    print("The volume of the star in meters cubed: "+str(volume))    
    sa=4*math.pi*(radius_actual**2) # surface area in meters squared
    print("The surface area of the star in meters squared: "+str(sa))    
    B=random.uniform(3.3,3.7)
    luminosity=mass**B # we are using the value of mass in relation to the sun's mass again for this part
    print("The luminosity of the star is "+str(luminosity)+" times the luminosity of the Sun")
    # Time to find the temperature
    # We need to convert the luminosity of the star into real units now
    l_actual=luminosity*3.828E26
    print("The luminosity of the star in watts: "+str(l_actual))
    temperature=(l_actual/(sa*sb))**(1/4) # Solved Stefan-Boltzmann law for temperature
                                          # sb is the Stefan-Boltzmann constant
    print("The surface temperature of the star in Kelvin: "+str(temperature))

    ####### Time to produce a blackbody radiation curve #######

    T=temperature
    x=np.arange(1,1000)
    Intensity=((2*h*c**2)/(x**5))(1/(exp(((h*c)/(x*k*T)-1))
    plt.plot(x, Intensity, '--', label='Intensity')
    plt.legend(loc='upper right')
    plt.title("Planck Blackbody Radiation Curve")
    plt.xlabel("Wavelength")
    plt.ylabel("Intensity")

    plt.show()

我正在尝试绘制给定恒星温度的黑体辐射曲线。我遇到的问题是 error。 Plot 函数上面的代码都是正确的并且可以工作,我把它留在里面以供参考。我确定这是一个简单的解决方法,但我不熟悉如何绘制这样的函数?我所有的导入和定义的变量都在代码之上。

有几个问题
Intensity=((2*h*c**2)/(x**5))(1/(exp(((h*c)/(x*k*T)-1))

括号不平衡,括号之间缺少*exp函数未定义。由于 xnp.ndarray 你需要使用 np.exp 而不是 math.exp,所以它应该是

Intensity = ((2 * h * c**2) / x**5) * (1 / (np.exp((h * c) / (x * k * T)) - 1))

然而,还应注意范围 (1, 1000) 在这种情况下相当无用,因为 x(波长)以 为单位,并且1 m 和 1 km 波长之间的辐射不是很有趣。它应该更像 (1e-8, 1e-6)(10 nm 到 1 um),它可以由 np.linspace(1e-8, 1e-6, 1000) 生成,然后给出正常的黑体曲线:


完整示例代码

import matplotlib.pyplot as plt
import numpy as np
import math

h = 6.62607015e-34
k = 1.380649e-23
c = 299792458.00
sb = (2 * math.pi**5 * k**4) / (15 * c**2 * h**3)

def one_star():
    mass = np.random.uniform(0.1, 50)
    radius = mass**0.9 * 6.96e8
    B = np.random.uniform(3.3, 3.7)
    sa = 4 * math.pi * radius**2
    T = (luminosity / (sa * sb))**(1/4)
    x = np.linspace(1e-8, 1e-6, 1000)
    I = ((2 * h * c**2) / x**5) * (1 / (np.exp((h * c) / (x * k * T)) - 1))
    plt.plot(x, I)

for i in range(5):
    one_star()
plt.show()