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

Putting It All Together

00:00 This is optimal view one. That numeric suffix hints that there’s more to come. But for now, let’s graph our target. These are the modules you need inside the get_viewing_times() function, which I showed you before.

00:12 And this is the pyplot module from Matplotlib. The pyplot module contains the functions for doing graphing and like with pandas and NumPy, the common practice is to alias it out.

00:24 Fair warning, there will be grumbling ahead.

00:27 Here I’m importing the constants that I defined in conf.py and just below that is the get_view_times() function that I walked you through before.

00:36 Let me scroll down past it. This is a bit of setup. Astropy’s visualization module has some features that make it easier to interact with Matplotlib. First off, it has a style spec to make the graph pretty.

00:49 And second, the quantity_support() function hooks Matplotlib so that it knows how to deal with values that have Astropy units attached.

00:58 In my conjunction script in the last lesson, I was a little naughty. If you had imported the file instead of running it, you would get side effects. Bare code in your file, stuff that isn’t in a function or class gets run when you run the file as a script, but it also gets run if you import the file.

01:15 If I wanted to reuse the get_viewing_times() function in this file and imported it elsewhere, I’d want to be careful about side effects.

01:24 This little if statement helps with that. The double underscore name variable, also known as __name__, gets set by Python itself and contains the name of the module.

01:34 If the file gets imported, __name__ contains the name of the file, but if it gets run as a script, it contains a string containing the word __main__.

01:45 By checking the value of __name__, you can make sure the code in the if block only runs if the program gets run as a script. For scripts you’re using on your own and that are just sort of for you, you don’t have to do this.

01:57 In fact, if I weren’t writing this as an example, I probably wouldn’t have bothered, but it is a best practice and this is a Python site, so I figured I should, you know, do the right thing. Inside the __name__ if block, the first thing you find is that core chunk of code that I showed you earlier that gets the data to be graphed. And speaking of it, now it’s time to do the graphing. Matplotlib is a funny little library.

02:23 It has two modes, context mode and object mode. In context mode, you don’t actually create a graph object at all. You just start calling functions. It automatically creates an object behind the scenes and any function call you use modifies that same object.

02:39 The computer science purist in me cringes at this idea. You can create graph objects instead if you like, but the majority of Matplotlib code out there uses this context-based approach.

02:51 So I’m doing what everyone else does. Something, something lemmings, something something. If Billy jumped off a bridge.

02:59 Anyhow, this first call to the pyplot module of Matplotlib is plot(), which is what gets used to create a line graph. The plt short form that I alias is often pronounced plot.

03:11 So if the context approach weren’t complicated enough, now you’ve got plot(), plot().

03:17 I believe I warned you about my grumbling. It’s not done yet.

03:22 The plot() call is responsible for drawing a line graph and it takes a few arguments. Its first one is the data for the x-axis, which is a range of naive viewing times.

03:33 The second argument is the y-axis data, which is the range of altitudes from our transformed alt class.

03:41 I then apply some color. M is for magenta or for magnificent depending on your taste. And finally, I label the line so that I can put it in the legend.

03:51 By default, Matplotlib will autoscale the x and y axes. I don’t want it auto-scaled, I want it to use my specific ranges. So I force that with the xlim() and ylim() calls.

04:03 The xlim() range is from minus three to 10. That’s the hours relative to midnight. While the ylim() is the earth’s hemisphere from negative 90 to positive 90 degrees.

04:14 The xlabel() and ylabel() calls label the x and y axes respectively.

04:21 And although legend() sounds like a really impressive thing, disappointingly, all it does is put a little table in our graph with the name of the line and that’s it.

04:29 With everything set up, you call the Matplotlib show() function. So yeah, even displaying the graph is based on a context call and it magically knows what graph you were talking about.

04:41 Back reference, grumbling something, something context-based calls. Alright, off to the command line

Become a Member to join the conversation.