Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding Black Body Radiation

Planck’s law dictates the relationship between the temperature of an object and the wavelengths of the radiation it emits. This lesson shows you how to graph the results of Planck’s law as a histogram using Matplotlib in order to visualize the wavelengths emitted by our sun.

00:00 In the previous lesson, I showed you how to use the pint library to add units to your calculations. In this lesson, I’ll be talking about black body radiation and how to graph the radiance of a star.

00:11 Black body is a theoretical thing that physicists use to think about how something absorbs energy. It’s called “Black” because this theoretical body absorbs all frequencies.

00:23 Black body radiation then is the stuff that a black body could spit back out if everything it absorbed was remitted. If you point a telescope at a star, you can measure what frequencies come out of a body and how much of each.

00:36 This gives you information about the star, and in fact, you can use these values to work out the surface temperature.

00:44 The relationship between the temperature of an object and the wavelengths of radiation that it emits can be determined using Planck’s law. This is a bit of a messy corner of physics.

00:55 If you go to Wikipedia and look up Planck’s law, you’ll find seven different formulas. They’re all interrelated and they have to do with whether you want to represent the radiation as frequency as a wave, and if it’s a wave, how you’re measuring the wave, and if that’s not bad enough, there’s radiance, irradiance, spectral density, and several other related terms.

01:15 All these things come back to Planck’s law. Some of the constants involved change, and fundamentally, from a layperson’s perspective, this just comes down to brightness for a given wavelength or frequency. This is the variation of Planck’s law using a wavelength measured in nanometers.

01:32 It isn’t as bad as it looks. The input is a wavelength and a temperature, and most of the stuff on the right-hand side is actually constants.

01:40 The units, on the other hand, are horrific. Radiance is expressed in watts per square, radiance per meter cubed,

01:48 but only for this version of the formula. You may also see times meters squared per nanometer, and if you’re doing the frequency thing, there’s a hertz in there somewhere.

01:57 Like I said, messy. Speaking of constants, h is Planck’s constant. c is the speed of light, and k is Boltzmann’s constant all substituted into this formula.

02:10 That’s the quick version of the physics. Now onto the code, What I’m going to show you how to do, is to create a black body radiation graph. This is a histogram showing the amount of radiance for a range of wavelengths for a body of a given temperature.

02:26 To do that, you need to write a function that encodes Planck’s law, then graph the intensity, and for fun, I’m also going to colorize the portion of the chart that’s in the visible spectrum.

02:38 Remember when I said there are lots of variations on Planck’s law? Well, I’ve run into contradictory sources when deciding which formula to use with the units of wavelength that I’ll be showing.

02:47 There’s a chance I’ve gotten the wrong one. This won’t be the only time you hear me say this, but if there’s a physics PhD who wants to tell me just how wrong I am in the comments, I look forward to finally being enlightened on how this is just supposed to work.

03:02 To take on the black body graph, you’re going to need a couple of libraries to help you out. The first is NumPy, which is a math and computation library. One of its core concepts is that it can handle multidimensional arrays and most of its math functions can be done on an array computing over all of the values.

03:20 NumPy is written in a lower-level language and Python connects down into it, which means it’s far faster than pure Python.

03:27 For our black body problem, I won’t be using very much NumPy, just two features. The first is the mathematical constant e. That also happens to be in the math module of the standard library, but I have to import NumPy anyways, so why import another module for the code?

03:44 The second and more important feature is the linspace() function. What this function does is return an array that’s a special object type in NumPy, similar to a list but numerically focused, that has a range of values. Given start, stop, and the number of arguments, the array will consist of num evenly spread values, beginning with start and ending with stop.

04:08 NumPy is a third-party library, so you use pip install to get it into your system. As always, you should be using a virtual environment when using third-party packages.

04:17 The next library I’ll be using is Matplotlib, which is a visualization module.

04:22 I’m going to use it to plot a histogram. The library has a hist() function, which makes creating one a single function call.

04:30 The graph on its own needs some labels, so there will also be calls for setting a title and the x and y axes. Finally, I want to change the color of just some of the bars of the histogram, those that have wavelengths in the visual spectrum.

04:45 Matplotlib allows you to get at the component parts of its graph using a mechanism it calls patches. For a histogram. patches are the rectangles in the histogram, the bars. Once you’ve got at the rectangles, you can call methods on them to manipulate them, which in this case, I’ll be using to change their colors.

05:04 Like with NumPy, you need to pip install Matplotlib as well.

Become a Member to join the conversation.