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

Adding to the Graph

00:00 Knowing where your target is is a good first step, but it’s not the only thing to consider. One other thing to worry about is bright lights. Hey, Beetlejuice, then gremlins.

00:10 Now all I have to do is make a Rambo joke and I’ll have completed my eighties movie bingo card. Two such bright lights are the sun and moon, which at risk of stating the obvious are also celestial bodies.

00:21 This means you can use Astropy to graph their locations as well. By adding that information to the graph, you can now see not only the position of the target, but also whether the sun or moon will get in your way.

00:33 Have you ever noticed that it’s still pretty bright out even after the sun goes down? That persistent glow in twilight drowns out most star observation. In fact, the sun has to get 18 degrees below the horizon before that effect is completely eliminated.

00:48 Let’s go add the sun and moon info to our plot.

00:53 This is optimal_view2, most of which is pretty similar to version one. In fact, I copied it over and then made some edits. Remember when I got on my high horse about best practices?

01:03 Yeah, well, this isn’t one of them. I was going to move the supporting functions into a utility, but well, then I got arrested by the sheriff because he didn’t like the length of my hair or my army jacket and I ran out of time.

01:14 See Rambo reference for the bingo win. Yeah, let me get back on track here. Scrolling down.

01:21 Wanting information about the sun is so common that Astropy provides a function for it. The result here after calling transform2() is the localized coordinates for the sun similar to those for a viewing target and you’ve seen get_body() before. This time instead of looking up planets, I’m looking up the moon.

01:40 Astropy also has a get_moon() call, but it’s been deprecated. You’re supposed to use get_body() now instead. Okay. Remember when I mentioned the whole it isn’t dark until negative 18 degrees thing?

01:51 Well, the matplotlib.axhspan() allows you to draw a rectangle on your graph. Here I’m drawing one that starts at minus 18 degrees and goes to zero, coloring it black, but making it 90% transparent.

02:05 This will mark out the twilight zone. I’d make a Rod Serling reference here, but that predates the eighties by two decades and even I’m not that old. With the twilight zone in place, I make another call to plot(), this time plotting the sun’s coordinates.

02:20 The linestyle parameter makes this a dashed line.

02:25 Then I do it again for the moon using a dot-dash style.

02:29 Remember, because Matplotlib is context-based. Each subsequent call to plot() is plotting a new line on the same magic hidden graph behind the scenes.

02:39 This time around the legend is more useful. Now you’ve got three lines and you’ll want to differentiate between them. With the new lines on the graph, you call show() and you get a new result.

02:50 I’ve skipped running it in the terminal this time and just put the results up here in the slide. The Andromeda curve in magenta is the same as before, but now I’ve added sun and moon lines as well.

03:02 This is actually useful. The fact that Andromeda peaks in the sky at 7:00 AM doesn’t make for good viewing at the time of year. I did this recording. The sun is 10 degrees above the horizon at 7:00 AM. If I trace the line of the sun backwards to where it’s below the twilight zone, you could see that 2:00 AM is probably a pretty good viewing time for Andromeda.

03:21 For me, the sun is below the zone as is the moon, and so the sky is dark. Andromeda isn’t as high in the sky, but at least I’ll be able to see it.

03:31 Just because the moon is in the sky doesn’t mean it will interfere with your viewing. Depending on its phase, you’ll get more or less illumination. As an optional little homework assignment, you could add information to the graph about the brightness of the moon.

03:45 I found this little nugget on the Astropy mailing list and it uses the right ascensions and declinations of the sun and the moon and a little bit of trigonometry to get you an illumination value.

03:56 You could add this as text on your graph or get fancy and try and have the moon label and the legend say something about new, quarter, half, or full, depending on its phase.

Become a Member to join the conversation.