This is just the basics with astroML, combined with an ipython notebook. These examples are shamelessly copied directly from the text. Then we'll move on to more interesting things.
First, let's use the pre-built astroML function to import a sample data set from SDSS.
from astroML.datasets import fetch_imaging_sample
data = fetch_imaging_sample()
How many lines does the data have?
data.shape
(330753,)
What data is contained in each line?
data.dtype.names[:]
('ra',
'dec',
'run',
'rExtSFD',
'uRaw',
'gRaw',
'rRaw',
'iRaw',
'zRaw',
'uErr',
'gErr',
'rErr',
'iErr',
'zErr',
'uRawPSF',
'gRawPSF',
'rRawPSF',
'iRawPSF',
'zRawPSF',
'upsfErr',
'gpsfErr',
'rpsfErr',
'ipsfErr',
'zpsfErr',
'type',
'ISOLATED')
To print a random sample of data. In this case the first 5 entries.
data['ra'][:5]
array([ 0.358174, 0.358382, 0.357898, 0.35791 , 0.358881])
Now, let's do something a little more interesting. Let's make a simple color-magnitude diagram. We have to use Matplotlib now.
%pylab inline
from matplotlib import pyplot as plt
stars = data[data['type'] == 6]
plt.plot(stars['gRaw'][:400]-stars['rRaw'][:400],stars['gRaw'][:400],'r.')
xlim(-1,3.5)
ylim(26,14)
xlabel("g-r")
ylabel("g")
plt.show()
Populating the interactive namespace from numpy and matplotlib
Maybe there's a bit of a main sequence there. It's a bit hard to tell because these are all field stars.
Now let's make a color-color diagram.
plt.plot(stars['gRaw']-stars['rRaw'],stars['rRaw']-stars['iRaw'],'r.',alpha=0.01)
plt.show()
But clearly these axes are wrong. When we zoom in:
plt.plot(stars['gRaw']-stars['rRaw'],stars['rRaw']-stars['iRaw'],'r.',alpha=0.01)
xlim(-1,3)
ylim(-1,2.5)
plt.show()
This is cluttered! Let's fix with a histogram.
import numpy as np
from astroML.plotting import scatter_contour
g = stars["gRaw"]
r = stars["rRaw"]
i = stars["iRaw"]
fig, ax = plt.subplots(figsize=(10,7.5))
scatter_contour(g - r, r - i, threshold=200, log_counts=True, ax=ax,
histogram2d_args=dict(bins=40),
plot_args=dict(marker=',', linestyle='none', color='black'),
contour_args=dict(cmap=plt.cm.bone))
ax.set_xlim(-0.6, 2.5)
ax.set_ylim(-0.6, 2.5)
plt.show()
But clearly there is a problem here. Why? The binning is too coarse. Let's see if we can fix this. What happens when we increase the number of bins from 40 to 200?
import numpy as np
from astroML.plotting import scatter_contour
g = stars["gRaw"]
r = stars["rRaw"]
i = stars["iRaw"]
fig, ax = plt.subplots(figsize=(10,7.5))
scatter_contour(g - r, r - i, threshold=200, log_counts=True, ax=ax,
histogram2d_args=dict(bins=200),
plot_args=dict(marker=',', linestyle='none', color='black'),
contour_args=dict(cmap=plt.cm.bone))
ax.set_xlim(-0.6, 2.5)
ax.set_ylim(-0.6, 2.5)
plt.show()