In [1]:
%config InlineBackend.figure_format = 'retina'
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
plt.rcParams.update({'font.size':16})

More fun with functions, dataframes and plotting

Overview:

Objectives

  • Writing functions to manipulate the data
  • Using apply()
  • Making some fancier plots

If you've completed all of the last notebook, been and had a cup of tea and watched Emmerdale, you'll need to start here by reading in the big_cepheids_table.csv file into a dataframe. Note: This isn't a file for you to download, you need to create it yourself.

In [2]:
cepheids_df = pd.read_csv('./data/big_cepheids_table.csv')
In [3]:
cepheids_df
Out[3]:
Star_ID parallax_mas logP mag_B err_B mag_V err_V mag_I err_I E_B_V A_V
0 XX Cen 0.564 1.040 8.882 0.019 7.855 0.012 6.754 0.008 0.5223 1.6191
1 T Mon 0.733 1.432 7.436 0.022 6.187 0.014 5.005 0.010 0.5997 1.8590
2 TW Nor 0.362 NaN NaN NaN NaN NaN NaN NaN NaN NaN
3 CV Mon 0.602 0.731 11.681 0.015 10.314 0.010 8.653 0.006 1.4373 4.4556
4 RY Sco 0.754 1.308 9.568 0.018 8.037 0.012 6.271 0.008 1.2228 3.7907
5 TT Aql 0.994 1.138 8.560 0.022 7.185 0.014 5.745 0.009 0.9672 2.9983
6 QZ Nor 0.471 0.578 9.782 0.007 8.875 0.004 7.871 0.003 1.2364 3.8329
7 Y Oph 1.349 1.234 7.573 0.011 6.161 0.007 4.543 0.005 0.8138 2.5227
8 VW Cen 0.256 1.177 11.754 0.022 10.306 0.014 8.802 0.009 1.3431 4.1638
9 V340 Nor 0.490 1.053 NaN NaN 8.407 0.005 NaN NaN 1.2831 3.9778
10 GY Sge 0.346 NaN NaN NaN NaN NaN NaN NaN NaN NaN
11 WZ Car 0.281 1.362 10.614 0.027 9.343 0.018 8.002 0.011 NaN NaN
12 CD Cyg 0.393 1.232 10.422 0.025 9.023 0.016 7.525 0.010 NaN NaN
13 FF Aql 1.920 0.650 6.159 0.007 5.383 0.005 4.503 0.004 0.4996 1.5486
14 Y Lac 0.430 0.636 9.921 0.016 9.163 0.011 8.312 0.007 0.3880 1.2028
15 BB Sgr 1.194 0.822 7.987 0.013 6.965 0.008 5.855 0.006 0.2405 0.7455
16 Z Lac 0.510 1.037 9.623 0.019 8.448 0.012 7.212 0.008 0.6702 2.0775
17 BG Lac 0.582 0.727 9.878 0.013 8.900 0.009 7.825 0.005 0.3374 1.0461
18 UU Mus 0.308 1.066 11.040 0.020 9.839 0.013 8.502 0.008 0.9659 2.9944
19 KN Cen 0.240 1.532 11.604 0.021 9.918 0.015 8.024 0.011 1.5576 4.8286
20 RU Sct 0.521 1.294 11.291 0.023 9.526 0.015 7.486 0.009 NaN NaN
21 DL Cas 0.581 NaN NaN NaN NaN NaN NaN NaN 0.7936 2.4602
22 RS Pup 0.587 1.617 8.580 0.019 7.057 0.013 5.507 0.009 0.8197 2.5412
23 CE Cas B 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440
24 SV Vul 0.405 1.653 8.810 0.018 7.267 0.012 5.719 0.009 NaN NaN
25 VY Car 0.564 1.277 8.744 0.015 7.510 0.012 6.301 0.007 1.0360 3.2115
26 V367 Sct 0.470 NaN NaN NaN NaN NaN NaN NaN NaN NaN
27 U Nor 0.622 1.102 10.939 0.020 9.273 0.013 7.366 0.008 NaN NaN
28 LS Pup 0.213 1.151 11.790 0.020 10.500 0.013 9.087 0.008 0.7634 2.3665
29 AQ Pup 0.290 1.479 10.197 0.022 8.756 0.015 7.178 0.010 0.7818 2.4236
... ... ... ... ... ... ... ... ... ... ... ...
37 CS Vel 0.274 0.771 13.071 0.017 11.728 0.011 10.080 0.007 1.9023 5.8970
38 V Cen 1.413 0.740 7.769 0.017 6.857 0.011 5.805 0.007 1.1718 3.6327
39 CF Cas 0.315 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440
40 T Vel 0.936 0.667 9.001 0.014 8.047 0.009 6.971 0.006 0.8183 2.5367
41 X Cyg 0.909 1.214 7.659 0.023 6.434 0.014 5.254 0.009 0.6879 2.1326
42 VZ Cyg 0.541 0.687 9.900 0.015 8.983 0.010 7.970 0.006 0.2543 0.7883
43 CE Cas A 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440
44 SW Vel 0.409 1.370 9.435 0.022 8.189 0.014 6.875 0.009 0.9399 2.9137
45 SZ Aql 0.521 1.234 10.227 0.025 8.697 0.016 7.103 0.010 1.3786 4.2736
46 S Vul 0.231 NaN NaN NaN NaN NaN NaN NaN 1.9095 5.9195
47 RZ Vel 0.660 1.310 8.408 0.026 7.170 0.017 5.898 0.011 1.1769 3.6484
48 GH Lup 0.864 0.967 8.851 0.004 7.632 0.002 6.360 0.002 1.1761 3.6460
49 RY Vel 0.377 1.449 9.872 0.021 8.421 0.014 6.846 0.009 1.4953 4.6355
50 X Sgr 2.822 0.846 5.334 0.013 4.569 0.009 3.663 0.006 NaN NaN
51 V496 Aql 0.976 0.833 8.937 0.008 7.769 0.005 6.491 0.004 0.3182 0.9863
52 V350 Sgr 0.806 0.712 8.449 0.015 7.504 0.010 6.453 0.006 0.3277 1.0158
53 U Vul 1.291 0.903 8.492 0.016 7.149 0.011 5.609 0.007 NaN NaN
54 W Sgr 2.354 0.881 5.472 0.017 4.694 0.011 3.863 0.007 0.5578 1.7292
55 SU Cyg 1.036 0.585 7.493 0.015 6.890 0.011 6.208 0.007 0.9989 3.0967
56 S Sge 1.685 0.923 6.488 0.016 5.641 0.011 4.782 0.006 0.2888 0.8953
57 beta Dor 2.917 0.993 4.586 0.013 3.758 0.009 2.946 0.006 0.0539 0.1669
58 U Car 0.559 1.589 7.625 0.025 6.342 0.016 5.076 0.010 NaN NaN
59 l Car 1.942 1.551 5.048 0.017 3.749 0.011 2.564 0.007 0.2190 0.6788
60 zeta Gem 3.064 1.007 4.735 0.011 3.901 0.007 3.100 0.005 0.0699 0.2166
61 Y Sgr 2.003 0.761 6.657 0.016 5.766 0.010 4.801 0.006 1.7647 5.4707
62 delta Cep 3.556 0.730 4.684 0.018 3.990 0.012 NaN NaN 1.4914 4.6233
63 U Aql 1.748 0.847 7.536 0.016 6.457 0.011 5.279 0.007 0.3334 1.0337
64 eta Aql 3.674 0.856 4.744 0.017 3.918 0.011 3.036 0.007 0.1936 0.6001
65 RT Aur 1.841 0.571 6.120 0.017 5.487 0.011 4.822 0.006 0.1844 0.5717
66 SU Cru 0.211 1.109 11.613 0.015 9.802 0.008 7.658 0.003 NaN NaN

67 rows × 11 columns

Finding the distance

cepheids_df has the parallax measurement for each star. Parallax is a way to measure distance using trigonometry.

parallax-.png

Gaia takes measurements of each star (almost 2 billion stars actually, but I'm not making you look at all of them, not this week anyway...) at different points in the year. It measures the apparent change in position of the nearby stars compared to a background of really distant objects (the angle $\theta$ in the figure). It also knows it's own position in the sky, so knows the length of the base of the triangle (2 astronomical units for observations taken six months apart). Because the angles involved are very small, the distance to the star in question goes to

$$ d = \dfrac{1}{\varpi}$$

where $d$ is in parsecs and $\varpi$ is the parallax angle ($\varpi = \theta/2$) in arcseconds. The parallax_mas column in cepheids_df is $\varpi$ in milli-arcseconds ($10^{-3}$ arcseconds).

Calculate the distance to each star

Using the equation for $d$ above, write a function to convert parallax (in milli-arcseconds) to distance (in parsecs).

Use your function to calculate the distance of each star in cepheid_df. Add a new column to the dataframe with a sensible column name that includes the units.

You should end up with your dataframe looking something like this with a new distance_pc column:

In [7]:
cepheids_df
Out[7]:
Star_ID parallax_mas logP mag_B err_B mag_V err_V mag_I err_I E_B_V A_V distance_pc
0 XX Cen 0.564 1.040 8.882 0.019 7.855 0.012 6.754 0.008 0.5223 1.6191 1773.049645
1 T Mon 0.733 1.432 7.436 0.022 6.187 0.014 5.005 0.010 0.5997 1.8590 1364.256480
2 TW Nor 0.362 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2762.430939
3 CV Mon 0.602 0.731 11.681 0.015 10.314 0.010 8.653 0.006 1.4373 4.4556 1661.129568
4 RY Sco 0.754 1.308 9.568 0.018 8.037 0.012 6.271 0.008 1.2228 3.7907 1326.259947
5 TT Aql 0.994 1.138 8.560 0.022 7.185 0.014 5.745 0.009 0.9672 2.9983 1006.036217
6 QZ Nor 0.471 0.578 9.782 0.007 8.875 0.004 7.871 0.003 1.2364 3.8329 2123.142251
7 Y Oph 1.349 1.234 7.573 0.011 6.161 0.007 4.543 0.005 0.8138 2.5227 741.289844
8 VW Cen 0.256 1.177 11.754 0.022 10.306 0.014 8.802 0.009 1.3431 4.1638 3906.250000
9 V340 Nor 0.490 1.053 NaN NaN 8.407 0.005 NaN NaN 1.2831 3.9778 2040.816327
10 GY Sge 0.346 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2890.173410
11 WZ Car 0.281 1.362 10.614 0.027 9.343 0.018 8.002 0.011 NaN NaN 3558.718861
12 CD Cyg 0.393 1.232 10.422 0.025 9.023 0.016 7.525 0.010 NaN NaN 2544.529262
13 FF Aql 1.920 0.650 6.159 0.007 5.383 0.005 4.503 0.004 0.4996 1.5486 520.833333
14 Y Lac 0.430 0.636 9.921 0.016 9.163 0.011 8.312 0.007 0.3880 1.2028 2325.581395
15 BB Sgr 1.194 0.822 7.987 0.013 6.965 0.008 5.855 0.006 0.2405 0.7455 837.520938
16 Z Lac 0.510 1.037 9.623 0.019 8.448 0.012 7.212 0.008 0.6702 2.0775 1960.784314
17 BG Lac 0.582 0.727 9.878 0.013 8.900 0.009 7.825 0.005 0.3374 1.0461 1718.213058
18 UU Mus 0.308 1.066 11.040 0.020 9.839 0.013 8.502 0.008 0.9659 2.9944 3246.753247
19 KN Cen 0.240 1.532 11.604 0.021 9.918 0.015 8.024 0.011 1.5576 4.8286 4166.666667
20 RU Sct 0.521 1.294 11.291 0.023 9.526 0.015 7.486 0.009 NaN NaN 1919.385797
21 DL Cas 0.581 NaN NaN NaN NaN NaN NaN NaN 0.7936 2.4602 1721.170396
22 RS Pup 0.587 1.617 8.580 0.019 7.057 0.013 5.507 0.009 0.8197 2.5412 1703.577513
23 CE Cas B 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3021.148036
24 SV Vul 0.405 1.653 8.810 0.018 7.267 0.012 5.719 0.009 NaN NaN 2469.135802
25 VY Car 0.564 1.277 8.744 0.015 7.510 0.012 6.301 0.007 1.0360 3.2115 1773.049645
26 V367 Sct 0.470 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2127.659574
27 U Nor 0.622 1.102 10.939 0.020 9.273 0.013 7.366 0.008 NaN NaN 1607.717042
28 LS Pup 0.213 1.151 11.790 0.020 10.500 0.013 9.087 0.008 0.7634 2.3665 4694.835681
29 AQ Pup 0.290 1.479 10.197 0.022 8.756 0.015 7.178 0.010 0.7818 2.4236 3448.275862
... ... ... ... ... ... ... ... ... ... ... ... ...
37 CS Vel 0.274 0.771 13.071 0.017 11.728 0.011 10.080 0.007 1.9023 5.8970 3649.635036
38 V Cen 1.413 0.740 7.769 0.017 6.857 0.011 5.805 0.007 1.1718 3.6327 707.714084
39 CF Cas 0.315 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3174.603175
40 T Vel 0.936 0.667 9.001 0.014 8.047 0.009 6.971 0.006 0.8183 2.5367 1068.376068
41 X Cyg 0.909 1.214 7.659 0.023 6.434 0.014 5.254 0.009 0.6879 2.1326 1100.110011
42 VZ Cyg 0.541 0.687 9.900 0.015 8.983 0.010 7.970 0.006 0.2543 0.7883 1848.428835
43 CE Cas A 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3021.148036
44 SW Vel 0.409 1.370 9.435 0.022 8.189 0.014 6.875 0.009 0.9399 2.9137 2444.987775
45 SZ Aql 0.521 1.234 10.227 0.025 8.697 0.016 7.103 0.010 1.3786 4.2736 1919.385797
46 S Vul 0.231 NaN NaN NaN NaN NaN NaN NaN 1.9095 5.9195 4329.004329
47 RZ Vel 0.660 1.310 8.408 0.026 7.170 0.017 5.898 0.011 1.1769 3.6484 1515.151515
48 GH Lup 0.864 0.967 8.851 0.004 7.632 0.002 6.360 0.002 1.1761 3.6460 1157.407407
49 RY Vel 0.377 1.449 9.872 0.021 8.421 0.014 6.846 0.009 1.4953 4.6355 2652.519894
50 X Sgr 2.822 0.846 5.334 0.013 4.569 0.009 3.663 0.006 NaN NaN 354.358611
51 V496 Aql 0.976 0.833 8.937 0.008 7.769 0.005 6.491 0.004 0.3182 0.9863 1024.590164
52 V350 Sgr 0.806 0.712 8.449 0.015 7.504 0.010 6.453 0.006 0.3277 1.0158 1240.694789
53 U Vul 1.291 0.903 8.492 0.016 7.149 0.011 5.609 0.007 NaN NaN 774.593338
54 W Sgr 2.354 0.881 5.472 0.017 4.694 0.011 3.863 0.007 0.5578 1.7292 424.808836
55 SU Cyg 1.036 0.585 7.493 0.015 6.890 0.011 6.208 0.007 0.9989 3.0967 965.250965
56 S Sge 1.685 0.923 6.488 0.016 5.641 0.011 4.782 0.006 0.2888 0.8953 593.471810
57 beta Dor 2.917 0.993 4.586 0.013 3.758 0.009 2.946 0.006 0.0539 0.1669 342.817964
58 U Car 0.559 1.589 7.625 0.025 6.342 0.016 5.076 0.010 NaN NaN 1788.908766
59 l Car 1.942 1.551 5.048 0.017 3.749 0.011 2.564 0.007 0.2190 0.6788 514.933059
60 zeta Gem 3.064 1.007 4.735 0.011 3.901 0.007 3.100 0.005 0.0699 0.2166 326.370757
61 Y Sgr 2.003 0.761 6.657 0.016 5.766 0.010 4.801 0.006 1.7647 5.4707 499.251123
62 delta Cep 3.556 0.730 4.684 0.018 3.990 0.012 NaN NaN 1.4914 4.6233 281.214848
63 U Aql 1.748 0.847 7.536 0.016 6.457 0.011 5.279 0.007 0.3334 1.0337 572.082380
64 eta Aql 3.674 0.856 4.744 0.017 3.918 0.011 3.036 0.007 0.1936 0.6001 272.182907
65 RT Aur 1.841 0.571 6.120 0.017 5.487 0.011 4.822 0.006 0.1844 0.5717 543.183053
66 SU Cru 0.211 1.109 11.613 0.015 9.802 0.008 7.658 0.003 NaN NaN 4739.336493

67 rows × 12 columns

Correcting for reddening and extinction

The dataframe contains two columns that tell us about reddening and extinction:

  • A_V is the extinction, $A_V$ - it's how much fainter an object appears in the V band
  • E_B_V is the colour excess, $E(B-V)$ - it tells us how much redder the object appears.

The colour excess is defined as

$$ E(B-V) = A_B - A_V$$

Combining these two pieces of information with some information about dust behaves, we can correct for extinction at any wavelength. The information about how dust interacts with light is contained in a reddening law. Reddening law's can get quite complex, but for our purposes we can approximate the reddening law at optical wavelengths as

$$ \dfrac{A_{\lambda}}{A_V} = \dfrac{0.7}{ \lambda} - 0.273 $$

where $A_{\lambda}$ is the extinction at wavelength $\lambda$, and $\lambda$ is in microns ($10^{-6} \text{ m}$).

The wavelengths of the three bands we're looking at are

  • $B = 0.445 \mu \text{m}$
  • $V = 0.551 \mu \text{m}$
  • $I = 0.806 \mu \text{m}$

To correct for extinction, all we have to do is subtract the extinction value from the corresponding magnitude, i.e.

$$ V_0 = V_{app} - A_V $$

and so on for $B$ and $I$.

(remember magnitudes go backwards, so subtracting the extinction makes the star brighter)

Calculate the extinction

Write a function that uses the $A_{\lambda}/ A_V$ equation and the wavelengths above, along with the $A_V$ values in the dataframe to calculate the extinction in the $B$ and $I$ bands.

Use your function to add new $A_B$ and $A_I$ columns to your dataframe.

You should end up with something like this:

In [12]:
cepheids_df.head()
Out[12]:
Star_ID parallax_mas logP mag_B err_B mag_V err_V mag_I err_I E_B_V A_V distance_pc A_B A_I
0 XX Cen 0.564 1.040 8.882 0.019 7.855 0.012 6.754 0.008 0.5223 1.6191 1773.049645 2.104885 0.964152
1 T Mon 0.733 1.432 7.436 0.022 6.187 0.014 5.005 0.010 0.5997 1.8590 1364.256480 2.416763 1.107009
2 TW Nor 0.362 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2762.430939 NaN NaN
3 CV Mon 0.602 0.731 11.681 0.015 10.314 0.010 8.653 0.006 1.4373 4.4556 1661.129568 5.792430 2.653249
4 RY Sco 0.754 1.308 9.568 0.018 8.037 0.012 6.271 0.008 1.2228 3.7907 1326.259947 4.928038 2.257310

Find the absolute magnitude

We now have all the ingredients we need to find the absolute magnitude of each star.

The absolute magnitude in the $V$ band is given by

$$ V_{abs} = V_{app} - A_V - \mu$$

and similar for the $B$ and $I$ bands.

$\mu$ is the distance modulus, defined as

$$\mu = 5 \log_{10} d - 5 $$

First we can define a function to calculate the distance modulus:

In [13]:
def dist_mod(distance):
    mu = 5.*np.log10(distance) - 5.
    return(mu)

We can define another function to calculate the absolute magnitude:

In [14]:
def abs_mag(app_mag, extinction, mu):
    absolute_mag = app_mag - extinction - mu
    return(absolute_mag)

Now we're going to use the apply function in pandas to calcuate the absolute magnitudes.

apply makes it easier to to more complex calculations on dataframes. One of the key parts of apply is the lambda parameter. Defining

apply(lambda x: ...)

means we can now refer to the dataframe just as x. This makes things much easier when we've got a lot of things to include.

We can now combine the functions to find the absolute magnitudes:

In [15]:
cepheids_df['V_abs'] = cepheids_df.apply(lambda x: abs_mag(x.mag_V, x.A_V, dist_mod(x.distance_pc)), axis=1)
In [16]:
cepheids_df
Out[16]:
Star_ID parallax_mas logP mag_B err_B mag_V err_V mag_I err_I E_B_V A_V distance_pc A_B A_I V_abs
0 XX Cen 0.564 1.040 8.882 0.019 7.855 0.012 6.754 0.008 0.5223 1.6191 1773.049645 2.104885 0.964152 -5.007704
1 T Mon 0.733 1.432 7.436 0.022 6.187 0.014 5.005 0.010 0.5997 1.8590 1364.256480 2.416763 1.107009 -6.346480
2 TW Nor 0.362 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2762.430939 NaN NaN NaN
3 CV Mon 0.602 0.731 11.681 0.015 10.314 0.010 8.653 0.006 1.4373 4.4556 1661.129568 5.792430 2.653249 -5.243618
4 RY Sco 0.754 1.308 9.568 0.018 8.037 0.012 6.271 0.008 1.2228 3.7907 1326.259947 4.928038 2.257310 -6.366843
5 TT Aql 0.994 1.138 8.560 0.022 7.185 0.014 5.745 0.009 0.9672 2.9983 1006.036217 3.897891 1.785447 -5.826368
6 QZ Nor 0.471 0.578 9.782 0.007 8.875 0.004 7.871 0.003 1.2364 3.8329 2123.142251 4.982899 2.282440 -6.592795
7 Y Oph 1.349 1.234 7.573 0.011 6.161 0.007 4.543 0.005 0.8138 2.5227 741.289844 3.279595 1.502233 -5.711640
8 VW Cen 0.256 1.177 11.754 0.022 10.306 0.014 8.802 0.009 1.3431 4.1638 3906.250000 5.413080 2.479486 -6.816600
9 V340 Nor 0.490 1.053 NaN NaN 8.407 0.005 NaN NaN 1.2831 3.9778 2040.816327 5.171274 2.368726 -7.119820
10 GY Sge 0.346 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2890.173410 NaN NaN NaN
11 WZ Car 0.281 1.362 10.614 0.027 9.343 0.018 8.002 0.011 NaN NaN 3558.718861 NaN NaN NaN
12 CD Cyg 0.393 1.232 10.422 0.025 9.023 0.016 7.525 0.010 NaN NaN 2544.529262 NaN NaN NaN
13 FF Aql 1.920 0.650 6.159 0.007 5.383 0.005 4.503 0.004 0.4996 1.5486 520.833333 2.013232 0.922170 -4.749094
14 Y Lac 0.430 0.636 9.921 0.016 9.163 0.011 8.312 0.007 0.3880 1.2028 2325.581395 1.563681 0.716251 -3.872458
15 BB Sgr 1.194 0.822 7.987 0.013 6.965 0.008 5.855 0.006 0.2405 0.7455 837.520938 0.969175 0.443935 -3.395478
16 Z Lac 0.510 1.037 9.623 0.019 8.448 0.012 7.212 0.008 0.6702 2.0775 1960.784314 2.700820 1.237123 -5.091649
17 BG Lac 0.582 0.727 9.878 0.013 8.900 0.009 7.825 0.005 0.3374 1.0461 1718.213058 1.359965 0.622938 -3.321485
18 UU Mus 0.308 1.066 11.040 0.020 9.839 0.013 8.502 0.008 0.9659 2.9944 3246.753247 3.892821 1.783124 -5.712646
19 KN Cen 0.240 1.532 11.604 0.021 9.918 0.015 8.024 0.011 1.5576 4.8286 4166.666667 6.277343 2.875365 -8.009544
20 RU Sct 0.521 1.294 11.291 0.023 9.526 0.015 7.486 0.009 NaN NaN 1919.385797 NaN NaN NaN
21 DL Cas 0.581 NaN NaN NaN NaN NaN NaN NaN 0.7936 2.4602 1721.170396 3.198343 1.465016 NaN
22 RS Pup 0.587 1.617 8.580 0.019 7.057 0.013 5.507 0.009 0.8197 2.5412 1703.577513 3.303646 1.513250 -6.641009
23 CE Cas B 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3021.148036 3.307286 1.514917 NaN
24 SV Vul 0.405 1.653 8.810 0.018 7.267 0.012 5.719 0.009 NaN NaN 2469.135802 NaN NaN NaN
25 VY Car 0.564 1.277 8.744 0.015 7.510 0.012 6.301 0.007 1.0360 3.2115 1773.049645 4.175058 1.912404 -6.945104
26 V367 Sct 0.470 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2127.659574 NaN NaN NaN
27 U Nor 0.622 1.102 10.939 0.020 9.273 0.013 7.366 0.008 NaN NaN 1607.717042 NaN NaN NaN
28 LS Pup 0.213 1.151 11.790 0.020 10.500 0.013 9.087 0.008 0.7634 2.3665 4694.835681 3.076530 1.409218 -5.224602
29 AQ Pup 0.290 1.479 10.197 0.022 8.756 0.015 7.178 0.010 0.7818 2.4236 3448.275862 3.150762 1.443221 -6.355610
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
37 CS Vel 0.274 0.771 13.071 0.017 11.728 0.011 10.080 0.007 1.9023 5.8970 3649.635036 7.666299 3.511583 -6.980247
38 V Cen 1.413 0.740 7.769 0.017 6.857 0.011 5.805 0.007 1.1718 3.6327 707.714084 4.722632 2.163223 -6.024989
39 CF Cas 0.315 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3174.603175 3.307286 1.514917 NaN
40 T Vel 0.936 0.667 9.001 0.014 8.047 0.009 6.971 0.006 0.8183 2.5367 1068.376068 3.297796 1.510570 -4.633321
41 X Cyg 0.909 1.214 7.659 0.023 6.434 0.014 5.254 0.009 0.6879 2.1326 1100.110011 2.772452 1.269934 -5.905781
42 VZ Cyg 0.541 0.687 9.900 0.015 8.983 0.010 7.970 0.006 0.2543 0.7883 1848.428835 1.024817 0.469422 -3.139314
43 CE Cas A 0.331 NaN NaN NaN NaN NaN NaN NaN 0.8207 2.5440 3021.148036 3.307286 1.514917 NaN
44 SW Vel 0.409 1.370 9.435 0.022 8.189 0.014 6.875 0.009 0.9399 2.9137 2444.987775 3.787908 1.735069 -6.666083
45 SZ Aql 0.521 1.234 10.227 0.025 8.697 0.016 7.103 0.010 1.3786 4.2736 1919.385797 5.555824 2.544870 -6.992411
46 S Vul 0.231 NaN NaN NaN NaN NaN NaN NaN 1.9095 5.9195 4329.004329 7.695550 3.524981 NaN
47 RZ Vel 0.660 1.310 8.408 0.026 7.170 0.017 5.898 0.011 1.1769 3.6484 1515.151515 4.743043 2.172572 -7.380680
48 GH Lup 0.864 0.967 8.851 0.004 7.632 0.002 6.360 0.002 1.1761 3.6460 1157.407407 4.739923 2.171143 -6.331431
49 RY Vel 0.377 1.449 9.872 0.021 8.421 0.014 6.846 0.009 1.4953 4.6355 2652.519894 6.026306 2.760377 -8.332793
50 X Sgr 2.822 0.846 5.334 0.013 4.569 0.009 3.663 0.006 NaN NaN 354.358611 NaN NaN NaN
51 V496 Aql 0.976 0.833 8.937 0.008 7.769 0.005 6.491 0.004 0.3182 0.9863 1024.590164 1.282223 0.587328 -3.270051
52 V350 Sgr 0.806 0.712 8.449 0.015 7.504 0.010 6.453 0.006 0.3277 1.0158 1240.694789 1.320574 0.604895 -3.980125
53 U Vul 1.291 0.903 8.492 0.016 7.149 0.011 5.609 0.007 NaN NaN 774.593338 NaN NaN NaN
54 W Sgr 2.354 0.881 5.472 0.017 4.694 0.011 3.863 0.007 0.5578 1.7292 424.808836 2.248018 1.029715 -5.176168
55 SU Cyg 1.036 0.585 7.493 0.015 6.890 0.011 6.208 0.007 0.9989 3.0967 965.250965 4.025814 1.844043 -6.129901
56 S Sge 1.685 0.923 6.488 0.016 5.641 0.011 4.782 0.006 0.2888 0.8953 593.471810 1.163920 0.533139 -4.121300
57 beta Dor 2.917 0.993 4.586 0.013 3.758 0.009 2.946 0.006 0.0539 0.1669 342.817964 0.216976 0.099387 -4.084218
58 U Car 0.559 1.589 7.625 0.025 6.342 0.016 5.076 0.010 NaN NaN 1788.908766 NaN NaN NaN
59 l Car 1.942 1.551 5.048 0.017 3.749 0.011 2.564 0.007 0.2190 0.6788 514.933059 0.882463 0.404216 -5.488554
60 zeta Gem 3.064 1.007 4.735 0.011 3.901 0.007 3.100 0.005 0.0699 0.2166 326.370757 0.281587 0.128982 -3.884156
61 Y Sgr 2.003 0.761 6.657 0.016 5.766 0.010 4.801 0.006 1.7647 5.4707 499.251123 7.112094 3.257727 -8.196295
62 delta Cep 3.556 0.730 4.684 0.018 3.990 0.012 NaN NaN 1.4914 4.6233 281.214848 6.010446 2.753112 -7.878491
63 U Aql 1.748 0.847 7.536 0.016 6.457 0.011 5.279 0.007 0.3334 1.0337 572.082380 1.343845 0.615554 -3.363993
64 eta Aql 3.674 0.856 4.744 0.017 3.918 0.011 3.036 0.007 0.1936 0.6001 272.182907 0.780150 0.357351 -3.856404
65 RT Aur 1.841 0.571 6.120 0.017 5.487 0.011 4.822 0.006 0.1844 0.5717 543.183053 0.743229 0.340440 -3.759431
66 SU Cru 0.211 1.109 11.613 0.015 9.802 0.008 7.658 0.003 NaN NaN 4739.336493 NaN NaN NaN

67 rows × 15 columns

Notice in the apply call above, we're creating a new column not just by running a function, but calling another function within that function. Doing all that while writing out the full dataframe name every time is just asking for mistakes.

There's lots of other uses for apply that you can read more about here. For now we'll stick with the simple case.

Use apply to find the $B$ and $I$ absolute magnitudes

Use the apply method shown above to calculate the $B$ and $I$ absolute magnitudes for each star. Add them as new columns to the dataframe.

Your dataframe should now have 67 rows and 17 columns:

In [19]:
cepheids_df.columns
Out[19]:
Index(['Star_ID', 'parallax_mas', 'logP', 'mag_B', 'err_B', 'mag_V', 'err_V',
       'mag_I', 'err_I', 'E_B_V', 'A_V', 'distance_pc', 'A_B', 'A_I', 'V_abs',
       'B_abs', 'I_abs'],
      dtype='object')

Fancy plotting

Our final task is to make a nice plot of the period-luminosity relation. When we did this with the Magellanic Clouds data before we were assuming all the stars were at the same distance. This time we've corrected for distance instead, and we'll be looking at the absolute magnitudes.

We can just make a quick plot:

In [20]:
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(1,1,1)
ax.plot(cepheids_df['logP'], cepheids_df['B_abs'], 'o', label='B')
ax.plot(cepheids_df['logP'], cepheids_df['V_abs'], 'o', label='V')
ax.plot(cepheids_df['logP'], cepheids_df['I_abs'], 'o', label='I')
ax.set_xlabel('log P (days)')
ax.set_ylabel('Absolute Magnitude')
ax.invert_yaxis()
ax.legend()
plt.show()

That doesn't look very nice. It doesn't look how I expect it to. Let's look at just one set of data for now -- the $I$ band data

In [21]:
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(1,1,1)
ax.plot(cepheids_df['logP'], cepheids_df['I_abs'], 'o', label='I')
ax.set_xlabel('log P (days)')
ax.set_ylabel('Absolute Magnitude')
ax.invert_yaxis()
ax.legend()
plt.show()

There's a lot more data in our dataframe than we're showing here. Perhaps something else is affecting the absolute magnitudes? My first guess is that it's extinction. We can look at this by colour coding the points using plt.scatter().

First we need to choose a colour map - this is done with get_cmap. You can see all the available colour maps here. I'm using the plasma colour map as it's a"perceptually uniform sequential" map - this means that a colour-blind person (or someone with a black and white printer) would still be able to interpret the colour scale.

We also need to have a colour bar so we can translate the colours into actual numbers when we look at the plot. We do this in two steps. First:

sc = ax.scatter(....

assigns the output of ax.scatter to the variable sc.

Then

cbar = plt.colorbar(sc)

makes the colour bar that corresponds to our data.

In [22]:
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(1,1,1)
cm = plt.cm.get_cmap('plasma')
sc = ax.scatter(cepheids_df['logP'], cepheids_df['I_abs'], c=cepheids_df.A_I, marker='o', label='I', cmap=cm, edgecolor='Grey')

ax.set_xlabel('log P (days)')
ax.set_ylabel('Absolute Magnitude')
ax.invert_yaxis()
ax.legend()

cbar = plt.colorbar(sc)
cbar.set_label('$A_I$')
plt.show()

Now we can see that the stars that had a larger extinction correction (the red to yellow points) seem to lie farther away from the trend than the blue points.

Make some fancy plots

Make similar plots to the one above, colour coding the points according to the different extinctions, the distances, or even the colour of the star (remember colour is the difference between two magnitudes, like (B-V)).

Make all the combinations you can think of! Do any of them look significant?

Make sure your plots have appropriate axis labels, units, etc, and write some markdown to explain what they are and any trends that they show.

Save your plots with savefig()

Save your data!

Finally, save your big dataframe. Be careful not to overwrite the original file, so give it a sensible filename so you will know what it is when you come back to it another day.

solution

Key Points:

  • Use apply to do fancy things with dataframes
  • Use plt.scatter and colourmaps to look at your data in multiple dimensions
  • Always save your data. In a sensible place. With a sensible filename
In [ ]: