DEMO: Black Body radiation#

import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interactive
from scipy.constants import h, c, k, e, N_A

%matplotlib inline
%config InlineBackend.figure_format='retina'
def planck(wavelength, T):
    """
    Planck's Law to calculate the spectral radiance of black body radiation at temperature T.
    
    Parameters:
    - wavelength (float): Wavelength in meters.
    - T (float): Absolute temperature in Kelvin.
    
    Returns:
    - (float): Spectral radiance in W/(m^2*sr*m).
    """
    return (2.0*h*c**2) / (wavelength**5 * (np.exp((h*c)/(wavelength*k*T)) - 1))

def plot_black_body(T=5800):
    """
    Plot the black body radiation spectrum for a given temperature T.
    """
    wavelengths = np.linspace(1e-9, 3e-6, 1000)  # Wavelength range from 1 nm to 3 um
    intensities = planck(wavelengths, T)

    plt.plot(wavelengths*1e9, intensities, label=f'T={T}')  # Convert wavelength to nm for plotting
    plt.xlabel('Wavelength (nm)')
    plt.ylabel('Intensity (W/m^2/sr/m)')
    plt.grid(True)
plot_black_body(T=4000)
plot_black_body(T=5000)
plot_black_body(T=6000)
plt.legend()
/var/folders/g4/3dkh7n1s0bjbl1tm3bgvvdsm0000gn/T/ipykernel_1512/2306792687.py:12: RuntimeWarning: overflow encountered in exp
  return (2.0*h*c**2) / (wavelength**5 * (np.exp((h*c)/(wavelength*k*T)) - 1))
<matplotlib.legend.Legend at 0x13f39db10>
../_images/9a4c5110979b2f62afd56606f6f917539695ddfb4e9ff8374356521b2ed92f88.png
interactive(plot_black_body, T=(1000, 10000, 100))