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

Graphing Black Body Radiation

00:00 This first chunk of output is for text. I print out the temperature and a table showing the wavelength and corresponding radiance,

00:08 and this chunk is the output of the graph. Remember when I said you can just start calling map plot functions and it builds things?

00:15 Well, this is the first one calling the hist() function, creating a histogram. The first argument to hist() is the wavelengths.

00:23 I’ve multiplied the contents here so that you don’t get really tiny little numbers. I’m essentially expressing it in nanometers. The second argument is the number of boxes in the histogram, and the third number is the actual radiance output.

00:37 The histogram call is a special version of a bar graph where if the data doesn’t match the number of bins in the histogram, the data gets resembled so that it does.

00:47 If you only wanted the histogram, you wouldn’t need to concern yourself with the return values from this function, but I want to mess with the component parts, so I capture the patches value.

00:57 I’ve used the underscores to indicate that I don’t care about the first and second values in the returned tuple from plt.hist().

01:07 As I mentioned in the slides, the patches are the geometric objects that make up the graph. For a histogram, that’s the rectangles. Here I’m looping through all of the rectangles and all of the wavelengths at the same time.

01:21 That’s what the zip() function does. It allows you to iterate on two equally-sized iterables. I then call the rainbow() function using the wavelength.

01:29 It returns a string with a color name, and then I use that color to change the face color of the rectangle. matplotlib breaks its geometric things down into two ideas, an edge and a face.

01:42 The edge is the line around the thing, which I hard coded to black, while the face is what would be called the fill color in most drawing tools.

01:51 You could write more optimized code for this because once you get past the end of the visible spectrum, the face color will always be black, but I was lazy and there aren’t that many bars, so I just let it burn the cycles. Almost there.

02:04 These three calls are more function calls in the pyplot module. This is what I mean by context-based rather than object-based. The histogram call creates a graph and the title(), xlabel() and ylabel() calls change that graph.

02:17 This feels very weird to me. In fact, there are ways of getting a graph object rather than being context-based, but you’ll find this kind of code for matplotlib in the wild a lot.

02:27 So I figured I’d stick with this way even if it makes me uncomfortable. The final context call is what actually displays the graph.

02:36 Alright, let’s head off to the command line and run this.

02:45 There’s the script and it prompts me for the temperature. I’ll enter 5,770 as that’s the surface temperature of our sun, and here is the result. Along the x-axis, you have wavelengths starting at zero and going up to 3000 nanometers.

03:04 Along the y-axis, you have from zero to two and a half times 10 to the power of 13. Yeah, note that little 1e13 in the top left corner to tell you that the y-axis is actually scaled.

03:16 Not surprisingly, a significant portion of the light from our sun is in the visible spectrum. That’s probably why it’s visible to us. Our eyes wouldn’t evolve to see something in the 3000 nanometer range because little of the light that we actually receive is in that range.

03:33 From the radiance of one star, I’ll be moving to the luminosity of many. In the next lesson, I’ll show you how to visualize the brightness of many stars using a Hertzsprung-Russell diagram.

Become a Member to join the conversation.