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

Overlaying the Spectral Lines

“Serena” Quasar’s sparcl ID: 719e7ea6-8b79-11ef-be93-525400f334e1

00:00 In the previous lesson, I showed you how to plunder web pages for data. In this lesson, you’ll start putting it all together by adding the spectral lines to the dashboard.

00:10 This lesson and the next are all about adding features to the dashboard. First, I’ll start with adding the spectral lines. Then in the next lesson, I’ll add interactivity.

00:20 To overlay the spectral lines, you’ll need to get the CSV data into a usable form for the dashboard. I’m going to use a dictionary. Then to keep things simple, I’m going to use just a subset of our spectral lines, and add those to the graph. In the background, I’m going to start marimo in edit mode once more, and I’ll meet you at the browser.

00:47 In my first attempt at the dashboard, I stuck all the stuff in a single cell. That’s going to get messy when I add the UI components later. So I’m going to start by separating the prep code from the graphing code.

00:59 I’m going to start by clicking the plus here to create a new cell, right-clicking to make sure that it’s Python.

01:11 Grabbing the setup code, pasting it up here, and then I’m going to rerun all of the cells using the button in the bottom right-hand corner here to make sure everything worked.

01:25 Looks good. So now it’s time to go get our spectral line data, which I’m gonna load from the CSV created in the last lesson.

01:39 The goal here is to have a switch for each line so that you can turn the spectral overlays on and off. That means you’ll need to associate the line with a switch, as well as the data.

01:50 This calls for a dictionary. Thankfully, Polars has a few methods for converting DataFrames into dictionaries. The one I want is rows_by_key.

02:08 To show you how this worked, let me pop up another cell and evaluate this. Running everything. What has happened here is each unique name in the first column has become a key.

02:21 That’s why I rearranged the order of the data in the previous lesson. Since there can be more than one piece of data for each key, Polars sets each value as a list.

02:31 If you think back to the original table, there are some rows for the same elements due to them having different weights, and then the whole Greek letter thing as an image problem.

02:40 For our purposes, I’m just going to use the first value in each of these. You could get fancier here and plot them all together. In fact, if you source this data elsewhere, there’s such thing as an elemental doublet, meaning you could have more data for each of these lines.

02:56 But like I said, let’s stick with simple for now. Now, to keep my dashboard clean, I’m gonna remove this evaluation cell.

03:07 Keeping with the theme of simple and clean, I’m not going to use all of those elements that are in that dictionary. I want to use a subset, so I’m gonna create a list of the ones I want. To

03:28 distinguish the spectral lines from the spectral data, I want to make them different colors, so I’m going to create a list of colors as well.

03:45 As a Canadian, it hurts me to type that that way. I’ve intentionally chosen a different number of colors than spectral lines. I want the code to be able to handle it if I have more of one or the other.

03:56 I’ll show you a little more on that in a second. Alright, let’s just make sure I haven’t screwed anything up syntax-wise, ran the cell no problem. With the setup done, now it’s time to modify the graph itself.

04:12 Matplotlib’s axvline() allows you to add a line to the graph. I want to do that for each of the spectral lines I’ve chosen. The non-redshifted wavelength of an emission is known as its rest wavelength.

04:25 Our graph currently starts at 350 nanometers, which our rest spectral lines are smaller than, so I’m going to adjust the boundary first. Changing this 3,500 to a thousand,

04:41 which right-shifts the graph. Now, to create the spectral lines, I want to loop over each of the subset.

04:55 For each line, I need a color. So that I don’t run out of colors, I’m going to use a mod system to grab whatever the next one from the color list is.

05:12 Now to get the wavelength of the spectral line from the map.

05:22 Remember, line_map has a list of tuples in it. I want the first tuple, and the wavelength is the first value in the tuple, and this is what plots the spectral line.

05:34 The first argument is the value to be plotted. Setting a label allows this to be included in a legend. The linestyle argument specifies what the line should look like.

05:45 Two hyphens here mean a dashed line. The final argument sets the color of the line. I’m going to modify the call to plot in order to add a label there as well.

05:58 And I noticed I misspelled label. Let me fix that and try to run. Isn’t demoing fun? It’s axvline. Dyslexia for the win. Trying again.

06:14 There’s the graph, and in the continued habit of mistakes, I forgot to add the legend. Let’s do that now.

06:28 And fourth time’s the charm? And there you go. You’ve got a graph, the LY element, the silicon, and the carbon four. Not bad, huh? As it stands, everything on this graph is at observation wavelengths.

06:45 In this next lesson, I’ll add the interactive bits and redshift our spectral lines. You’re almost there. The next lesson finishes off the dashboard.

Become a Member to join the conversation.