# Python: blackbody curves

I decided to put a little bit of python code together as a demonstration of how to define a function by calculating the blackbody curve of an object (see the bottom of the entry if you want to know what this is), anyway I thought I’d share this little snippet of code.. its not anything particularly outstanding but tis neat. How to define a blackbody:

def blackbody_at_f_t(freq,kB,T,c,h): #T in K, f in Hz

return 2*h*(freq**3)/(c**2)*(1/(exp((h*freq)/(kB*T))-1))

You just need to pass freq – the observing frequency (in Hz), kB – the Boltzmann constant (in J/K), T – the temperature (in K), c – the speed of light (in m/s) and h – the Planck constant (in JS). This show just how simple python can be to define a function. Below is an example of actually using this, and of course we can nicely vectorise things too… and plot!

def blackbody_at_f_t(freq,kB,T,c,h): #T in K, f in Hz

return 2*h*(freq**3)/(c**2)*(1/(exp((h*freq)/(kB*T))-1))

#Python Libraries

from pylab import *

from scipy import *

from numpy import *

#constants

kB=1.38066E-23; # Boltzmann’s constant in J/K

c=3.0E8

h = 6.63E-34 #Planck’s constant (JS)

logspace = 10.**linspace(1.0, 20, 5000) #logspacing of frequency

T =2.715 #K cosmic background radiation

out = blackbody_at_f_t(logspace,kB,T,c,h)

fig = plt.figure(figsize=(6, 6))

ax1 = fig.add_subplot(111)

plot(logspace/1E9,out,’r-‘,markersize=0.5)

ax1.set_xlabel(‘Frequency (GHz)’, fontsize=10); ax1.set_ylabel(‘Energy Density (J/Hz)’, fontsize=10)

xlim(0.0,800)

show()

The whole above code can be found in [blackbody.py]

and you end up with blackbody curve of the cosmic microwave background:

A black body is a theoretical object that absorbs 100% of the radiation that hits it, hence it appears to be black. Due to this nature its also a perfect emitter of thermal radiation. which it radiates in a characteristic spectrum that depends on the body’s temperature – as can be seen above for the cosmic microwave background. If you want to know more about the cosmic microwave background try [here]

## Recent Comments