Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SWEETCat in python with PyAstronomy
This small example that shows how to use python to work with SWEET-Cat
It use the PyAstronomy package which can be found here
The package can be installed with pip
pip install PyAstronomy
or from source
git clone
cd PyAstronomy
python install --with-ext
Check the dependencies of PyAstronomy if there is problems.
You may have to remove and reinstall PyAstronomy if it's already on your system.
from PyAstronomy import pyasl
import matplotlib.pyplot as plt
# Create the SWEETCat object. This download SC to your local computer.
# SC is downloaded automatically if the file is more than 7 days old.
sc = pyasl.SWEETCat()
# If you want to force a fresh update, run
# Extract the data. This is a pandas DataFrame
data =
# Print the top of the table and the attributes
print data.head()
print data.keys()
# Let's plot the V mag vs. the effective temperature
data.plot('teff', 'vmag', kind='scatter')
# We can also create another column for our table, e.g. luminosity so we can make a HR diagram
# Note that this works even though we have missing values
data['lum'] = (data.teff/5777)**4 * data.mass
data.plot('teff', 'lum', kind='scatter', xlim=(8500, 2500))
# If we only are interested in a shorter table, with stars with Teff below 5000K, we can do so
data_cool = data[data.teff < 5000]
# See that it worked (print rows and columns)
print data.shape
print data_cool.shape
# And the plot
data.plot('teff', 'lum', kind='scatter', xlim=(8500, 2500))
plt.plot(data_cool.teff, data_cool.lum, 'oy')

This comment has been minimized.

Copy link
Owner Author

commented Aug 30, 2016

To search by HD number

print data.columns  # To be sure that "hd" is one of the columns
data[data.hd == number]

This comment has been minimized.

Copy link

commented Sep 15, 2016

Thanks it worked a charm.
To get a single value out of the the selected row of a pandas dataframe I needed to use the .iloc method suggested on stackoverflow
E.g. to get the temperature value of HD4747

sc = pyasl.SWEETCat()
data =
hd_number = 4747
if hd_number in
    hd_entry = data[data.hd == hd_number]    
    teff = hd_entry.iloc[0]["teff"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.