Loading video player…

Using Astropy for Astronomy With Python (Overview)

This course covers two problems from introductory astronomy to help you play with some Python libraries. You’ll use Astropy, NumPy, Matplotlib, and pandas to find planet conjunctions, and graph the best viewing times for a star.

In this course you’ll learn about:

  • Astronomy concepts of conjunction and optimal viewing
  • The Python package Astropy
  • Using pandas to process data
  • Building graphs with Matplotlib
  • Python’s warning module
Download

Course Slides (.pdf)

11.8 MB
Download

Sample Code (.zip)

4.5 KB

00:00 Welcome to using Astropy for astronomy in Python. My name is Christopher, and I will be your guide. This course was built in conjunction with an astrophysics student named Juliano.

00:12 So although you’ll only hear my voice, the material was a joint effort. This course is a little different from your usual Real Python single topic course.

00:21 Here the focus is on two problems in the astronomy space. That doesn’t mean you should only take it if you’re a stargazer. There are plenty of opportunities to learn some Python, but the approach is from a problem domain perspective rather than from the tool domains.

00:36 There are several third-party libraries available in Python for doing astronomical calculations. This course uses Astropy. If you’re new to astronomy, don’t worry, I’ll cover both the concepts and how Astropy represents them.

00:52 Doing science often requires dealing with lots of data, and as such, learning data management can be a real help. Although this isn’t a pandas course per se, I’ll be covering how you use pandas for the problems covered here, and you don’t need to know any pandas before I get started.

01:08 And same goes for Matplotlib, which is probably the most popular Python library out there for graphing data.

01:15 Astropy can be a little noisy at times, so along the way, I’ll also show you how to use Python’s built-in warnings module to suppress warnings.

01:25 The code in this course was tested with Python 3.12. You can get away with an earlier version, but I do use the zoneinfo module, which was added in Python 3.9, so you’ll probably want to use at least that.

01:38 Besides that, I used Astropy 6.1.0.1, Matplotlib 3.9.1.26, 4, pandas 2.2, and Tabulate 0.9.

01:53 You don’t have to be an amateur astronomer to enjoy the beauty of the night sky. Granted, I live in Toronto, a city of almost 3 million people with a surrounding area having another seven.

02:03 Oftentimes, the brightest light in my night sky is a helicopter, but travel a few hours north and you hit the wilderness and the sky just blooms stars.

02:12 Astronomy is a very accessible science anyone can look up and enjoy, but on top of that, most of the major institutions that do work in this space make their data publicly available.

02:22 As such, with a little bit of work or the right app, you can learn a lot about what you’re looking at. Python is the preeminent language for doing stuff quickly, and a lot of science work uses it.

02:34 In fact, there are several third-party libraries that can help you do things like better plan your viewing or just better understand how the stars move in the sky.

02:44 This course is a Python course with a flavor of astronomy. You’ll learn a bit about stellar coordinate systems and write some code to determine when there are planetary conjunctions and when best to view any given stellar body from your backyard.

02:58 Unfortunately, I can’t help you with the helicopters, but I bet if you Googled around, you could find a Python library that would. Next up, I’ll start by working out the motions of the planets and when they line up in the sky.

Become a Member to join the conversation.