I'm a fourth-year computer science student at the University of Lethbridge. Alongside my coursework I've been doing X-ray astronomy research: turning raw telescope data into plots that show how neutron star binaries behave over time.
This post covers what I built, how the data works, and the bug that taught me to check my plots against my expectations.
The goal
Neutron star low-mass X-ray binaries are systems where a neutron star pulls gas off a companion star. That gas heats up and shines in X-rays, and the brightness and "colour" of that light change as the system moves between states.
I studied four of the brightest ones:
| Source | NICER observations overlaid |
|---|---|
| Cyg X-2 | 3 |
| Aql X-1 | 2 |
| 4U 1608-52 | 1 |
| Sco X-1 | none |
For each source I wanted four plots:
- A light curve: X-ray intensity against time.
- Hardness ratios over time: how the high-energy part of the spectrum compares with the lower-energy part.
- A colour–colour diagram: hard ratio against soft ratio.
- A hardness–intensity diagram: 4–10 keV intensity against soft ratio.
Then I overlaid pointed observations from NICER, an X-ray telescope on the International Space Station, on top of the long-term tracks. That shows where each short NICER observation sits within the source's overall behaviour.
The data
The long-term data comes from MAXI, an all-sky X-ray monitor that has been running on the ISS since 2009. Its public archive gives you a plain-text file per source with nine columns:
| Column | Quantity |
|---|---|
| 1 | Time (MJD) |
| 2, 3 | 2–20 keV rate and error |
| 4, 5 | 2–4 keV rate and error |
| 6, 7 | 4–10 keV rate and error |
| 8, 9 | 10–20 keV rate and error |
That's about 16 years of measurements per source, which is a lot of points to plot well.
One detail that mattered: matching the instruments
My first instinct was to use the same ratios everywhere. The problem is that NICER's sensitivity drops off around 12 keV, so it can't measure the 10–20 keV band that MAXI's hard ratio depends on.
So for the NICER comparisons I didn't use the hard ratio at all. I used the 4–10 keV intensity against the soft ratio, which both instruments can measure. Comparing two telescopes only makes sense on quantities they can both actually see.
The bug
My early version of the Cyg X-2 notebook was meant to read only the lines for two specific dates, MJD 60537 and 60549. I wrote this:
for line in f:
if '60537' or '60549' in line:
soft = line.split(' ')[1]
...
It ran without errors and produced a plot. But when I went back through this notebook while cleaning up the project, I realised it was reading every line in the file, not just the two dates.
Python reads that condition as:
if ('60537') or ('60549' in line):
A non-empty string like '60537' is always truthy, so the condition is always True. What I meant was:
if '60537' in line or '60549' in line:
or, more cleanly:
wanted = ('60537', '60549')
if any(mjd in line for mjd in wanted):
The lesson I took from it: before trusting a plot, estimate roughly what it should look like. How many points? What range? If it doesn't match, something upstream is wrong, even if the code runs without errors.
If I wrote that parsing code today, I'd skip the hand-rolled string splitting and load the whole file with NumPy, which makes the column indices explicit:
import numpy as np
data = np.loadtxt("CygX-2data1.dat")
mjd = data[:, 0]
soft_band = data[:, 3] # 2–4 keV
mid_band = data[:, 5] # 4–10 keV
hard_band = data[:, 7] # 10–20 keV
hard_ratio = hard_band / mid_band
The result
Each notebook produces the light curve, hardness ratios, colour–colour diagram and hardness–intensity diagram, with NICER observations plotted on top where they exist.
What I learned
- Know your instruments. The physics limits of a telescope decide which comparisons are valid.
- Plots can lie quietly. Code that runs isn't code that's correct. Sanity-check output against what you expect.
-
Learn the language's traps.
x or y in zis a classic Python mistake, and now I spot it in other people's code too. -
Use the right tool.
np.loadtxtand column slicing are clearer than manual string parsing for tabular science data.
Tools
Python, NumPy, Matplotlib, Jupyter, and the public MAXI/GSC archive.
If you work with astronomical time series and have tips for plotting 16 years of noisy data nicely, I'd love to hear them in the comments.

Top comments (1)
16 years of X-ray data nearly outsmarted by one sneaky Python bug — every computational scientist just felt a chill down their spine! Huge respect for documenting the debugging trail. (Also if you ever submit Matplotlib PDFs to journals, watch out for hidden Type 3 bitmap fonts — tiny zero-dep CLI check: pip install alcyone-fig-qa)