%matplotlib inline from __future__ import division import numpy as np import matplotlib.pyplot as plt import pandas as pd # This IPython magic generates a table with version information #https://github.com/jrjohansson/version_information %load_ext version_information %version_information numpy, matplotlib, pandas filename = '../Hipparcos/I_239_selection.tsv' df = pd.read_table(filename, skiprows=44, sep=';', header=None, index_col=0, names = ['HIP', 'Vmag', 'Plx', 'B-V', 'SpType'], skipfooter=1, engine='python') df.head() df.tail() df.describe() df_clean = df.applymap(lambda x: np.nan if isinstance(x, basestring) and x.isspace() else x) df_clean.describe() df_clean= df_clean.dropna() df_clean.describe() df_clean.shape df_clean['Vmag'] = df_clean['Vmag'].astype(np.float) df_clean['Plx'] = df_clean['Plx'].astype(np.float) df_clean['B-V'] = df_clean['B-V'].astype(np.float) # Add a new column with the absolute magnitude df_clean['M_V'] = df_clean['Vmag'] + 5 * np.log10(df_clean['Plx']/100.) df_clean.head() # Rows that do not meet the condition alpha + num are eliminated f = lambda s: (len(s) >= 2) and (s[0].isalpha()) and (s[1].isdigit()) i = df_clean['SpType'].apply(f) df_clean = df_clean[i] # A new column is created with the first two characters from 'SpType' f = lambda s: s[0:2] df_clean['SpType2'] = df_clean['SpType'].apply(f) df_clean.shape df_clean.head() f = lambda s: s[0] clases = df_clean['SpType'].map(f) clases.value_counts() f = lambda s: s[0] in 'OBAFGKM' df_clean = df_clean[df_clean['SpType'].map(f)] f = lambda s: s[0] clases = df_clean['SpType'].map(f) clases.value_counts() orden = {'O':'0', 'B':'1', 'A':'2', 'F':'3', 'G':'4', 'K':'5', 'M':'6'} f = lambda s: orden[s[0]]+s[1] df_clean['SpType2'] = df_clean['SpType2'].apply(f) df_clean.head() fig, ax = plt.subplots(figsize=(8,10)) ax.set_xlim(0, 70) ax.set_ylim(15, -10) ax.grid() ax.set_title('H-R Diagram \n (Hipparcos catalog)') ax.title.set_fontsize(20) ax.set_xlabel('spectral class') ax.xaxis.label.set_fontsize(20) ax.set_ylabel('Absolute magnitude') ax.yaxis.label.set_fontsize(20) ax.scatter(df_clean['SpType2'].astype(np.int), df_clean['M_V'], s=50, edgecolors='none', alpha=0.015, c='k') ax.set_xticks(range(5,75,10)) ax.set_xticklabels(['O', 'B', 'A', 'F', 'G', 'K', 'M']) ax.tick_params(axis='both', labelsize=14) fig, ax = plt.subplots(figsize=(8,10)) ax.set_xlim(-0.5, 2.5) ax.set_ylim(15, -10) ax.grid() ax.set_title('H-R Diagram \n (Hipparcos catalog)') ax.title.set_fontsize(20) ax.set_xlabel('Color index B-V') ax.xaxis.label.set_fontsize(20) ax.set_ylabel('Absolute magnitude') ax.yaxis.label.set_fontsize(20) ax.scatter(df_clean['B-V'], df_clean['M_V'], # s=50, edgecolors='none', alpha=0.015, c='k') s=1, edgecolors='none', c='k') ax.tick_params(axis='both', labelsize=14) f = lambda s: 'VII' in s b = df_clean['SpType'].map(f) print "Class VII: white dwarfs, there are %d stars" %sum(b) f = lambda s: ('VI' in s) and ('VII' not in s) b = df_clean['SpType'].map(f) print "Class VI: subdwarfs, there are %d stars" %sum(b) f = lambda s: ('V' in s) and ('VI' not in s) and ('IV' not in s) b = df_clean['SpType'].map(f) print "Class V: main-sequence, there are %d stars" %sum(b) f = lambda s: 'IV' in s b = df_clean['SpType'].map(f) print "Class IV: subgiants, there are %d stars" %sum(b) f = lambda s: 'III' in s b = df_clean['SpType'].map(f) print "Class III: giants, there are %d stars" %sum(b) f = lambda s: ('II' in s) and ('III' not in s) and ('VII' not in s) b = df_clean['SpType'].map(f) print "Class II: bright giants, there are %d stars" %sum(b) f = lambda s: ('I' in s) and ('II' not in s) and ('V' not in s) b = df_clean['SpType'].map(f) print "Class I: supergiants, there are %d stars" %sum(b) f = lambda s: ('I' not in s) and ('V' not in s) b = df_clean['SpType'].map(f) print sum(b) def plot_lum_class(b,c, label): ''' b: boolean Series to make the selection c: Color label: for the legend ''' x = df_clean['B-V'][b] y = df_clean['M_V'][b] ax.scatter(x, y, c = c, s=6, edgecolors='none', label = label) fig = plt.figure(figsize=(8,10)) ax = fig.add_subplot(111, axisbg='0.6') ax.set_xlim(-0.5, 2.5) ax.set_ylim(15, -15) ax.grid() ax.set_title('H-R Diagram \n (Hipparcos catalog)') ax.title.set_fontsize(20) ax.set_xlabel('Color index B-V') ax.xaxis.label.set_fontsize(20) ax.set_ylabel('Absolute magnitude') ax.yaxis.label.set_fontsize(20) f = lambda s: 'VII' in s b = df_clean['SpType'].map(f) plot_lum_class(b,'white', 'VII: white dwarfs') f = lambda s: ('VI' in s) and ('VII' not in s) b = df_clean['SpType'].map(f) plot_lum_class(b,'blue', 'VI: subdwarfs') f = lambda s: ('V' in s) and ('VI' not in s) and ('IV' not in s) b = df_clean['SpType'].map(f) plot_lum_class(b,'black', 'V: main-sequence') f = lambda s: 'IV' in s b = df_clean['SpType'].map(f) plot_lum_class(b,'grey', 'IV: subgiants') f = lambda s: 'III' in s b = df_clean['SpType'].map(f) plot_lum_class(b,'green', 'III: giants') f = lambda s: ('II' in s) and ('III' not in s) and ('VII' not in s) b = df_clean['SpType'].map(f) plot_lum_class(b,'orange', 'II: bright giants') f = lambda s: ('I' in s) and ('II' not in s) and ('V' not in s) b = df_clean['SpType'].map(f) plot_lum_class(b,'yellow', 'I: supergiants') ax.tick_params(axis='both', labelsize=14) legend = ax.legend(scatterpoints=1,markerscale = 6, shadow=True) frame = legend.get_frame() frame.set_facecolor('0.90')