<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 AST 337 In-Class Lab #1
Wednesday, September 6, 2017
In this lab, you'll learn to read in and manipulate tabular data with the python package pandas and plot that data with the plotting module matplotlib.pyplot. 
On the science end, you will compare H-R diagrams for two different open star clusters, a globular cluster, and a  population of field (non-cluster) stars.
Step1:  First off, we'll need to read in the data with the pandas function read_csv. A basic example is given below
Step2:  "pleiades" is now a pandas dataframe object, which is essentially a form of python table. To see what's stored in pleiades, execute the cell below. Anywhere you see ..., that means that there are a number of additional columns or rows that have been hidden.
Step3:  Perhaps more useful are the pandas .columns and .dtypes methods. Execute the cells below and then edit the descriptions of what each does in the cell below (double click on this text to get into the markdown cell, where you can type regular text)
(1) The .columns method does....
(2) The .dtypes method does....
Step4:  The two columns that we care about for this lab are the Temperature (Teff) and the Luminosity (Lbol). The units for these two columns are Kelvin and Solar luminosities, respectively. As you will label these later in your plots, I'll note here that there's a special trick for getting the sun symbol in a Markdown cell using the typesetting system LaTeX. 
Solar Luminosities = L$_{\odot}$
Double click on this cell to see how I made the symbol above. 
Let's create a pandas series that we can manipulate from each of the columns of interest.
Step5:  Note though from your .dtypes output above that both of these columns have dtype "object", which is not a data type that will allow us to manipulate them. For example, try executing the cell below, where we attempt to subtract the value 2 from each entry in the "pleiades_L" column. You should get an error...
Step6:  So we want to convert the type of these pandas series to be numeric, which we do with pandas.to_numeric, as below
Step7:  Now let's print the first ten elements of each array to verify that nothing weird happened during this conversion.
Step8:  Now let's plot these two quantities against one another. There are many ways to plot in pyplot, but I'll use the one that I find to work most consistently and intuitively below.
Step9:  Yikes, that's ugly, right? That's because the default plot symbol is a line connecting all the points. In this case what we really want is a so-called scatterplot, which we can do easily by specifying the plotting marker right after the y variable, as below. Below I use 'o', which stands for the circle symbol. For the full list of matplotlib symbols, see this link.
Step10:  Note the default plotting color is blue, but you can change this easily by adding a color shorthand before the marker shorthand. Below I use 'g' for green, but here again, there are lots of options, as outlined at this link.
Step11:  Colors notwithstanding, this plot is still very ugly and should not look much like an H-R diagram to you. For one thing, H-R diagrams usually have log(Luminosity) on the y-axis, which you do as follows
Step12:  OK that's much nicer, but should still look backwards to you, because we always draw H-R Diagrams with the Temperature axis running from high to low temperature. This is a pretty easy fix too.
Step13:  OK! This is starting to look like a good H-R diagram, but without a plot title or axis labels, it's still not very good, so let's add those.
Step14:  Exercise 1
Now it's time for you to do some exploring with the rest of the data. For each of the remaining four data files in the Lab 1 directory
Step15:  Exercise 2 (Multi-Panel Plots)
The individual plots for each of these clusters are nice, but really we'd like to be able to compare them side-by-side. You can do this by making either a multi-panel plot, or an overlapping plot. A skeleton outline of how to do each is below. Please use this as a framework to make similar plots with our data.
Step16:  Exercise 3 (Comprehension Questions)
1) Describe the differences between the H-R diagrams. Do this both qualitatively (note differences in their appearance) and quantitatively. You might find methods like .min and .max to be useful here.
2) Which of the four groups of stars has stars that are NOT on the main sequence, and which don't? Why, do you think?
3) Why do you think the low temperature (M-dwarf) end of the main sequence cuts off at such a different place in the four samples? Do you think this is a physical effect or an instrumental one and why?
4) Why do you think there are no white dwarfs in any of the samples besides the field sample?
<ASSISTANT_TASK:>
Python Code:
#load packages
import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Explanation: AST 337 In-Class Lab #1
Wednesday, September 6, 2017
In this lab, you'll learn to read in and manipulate tabular data with the python package pandas and plot that data with the plotting module matplotlib.pyplot. 
On the science end, you will compare H-R diagrams for two different open star clusters, a globular cluster, and a  population of field (non-cluster) stars.
End of explanation
pleiades = pd.read_csv('pleiades.csv')
Explanation: First off, we'll need to read in the data with the pandas function read_csv. A basic example is given below:
End of explanation
pleiades
Explanation: "pleiades" is now a pandas dataframe object, which is essentially a form of python table. To see what's stored in pleiades, execute the cell below. Anywhere you see ..., that means that there are a number of additional columns or rows that have been hidden.
End of explanation
pleiades.columns
pleiades.dtypes
Explanation: Perhaps more useful are the pandas .columns and .dtypes methods. Execute the cells below and then edit the descriptions of what each does in the cell below (double click on this text to get into the markdown cell, where you can type regular text)
(1) The .columns method does....
(2) The .dtypes method does....
End of explanation
pleiades_L = pleiades["Lbol"]
pleiades_T = pleiades["Teff"]
Explanation: The two columns that we care about for this lab are the Temperature (Teff) and the Luminosity (Lbol). The units for these two columns are Kelvin and Solar luminosities, respectively. As you will label these later in your plots, I'll note here that there's a special trick for getting the sun symbol in a Markdown cell using the typesetting system LaTeX. 
Solar Luminosities = L$_{\odot}$
Double click on this cell to see how I made the symbol above. 
Let's create a pandas series that we can manipulate from each of the columns of interest.
End of explanation
pleiades_L = pleiades_L - 2
Explanation: Note though from your .dtypes output above that both of these columns have dtype "object", which is not a data type that will allow us to manipulate them. For example, try executing the cell below, where we attempt to subtract the value 2 from each entry in the "pleiades_L" column. You should get an error...
End of explanation
pleiades_L_new = pd.to_numeric(pleiades_L, errors='coerce')
pleiades_T_new = pd.to_numeric(pleiades_T, errors='coerce')
# With "coerce", we are telling the to_numeric function to change any invalid entries to NaNs.
Explanation: So we want to convert the type of these pandas series to be numeric, which we do with pandas.to_numeric, as below:
End of explanation
pleiades_L[0:10], pleiades_L_new[0:10], pleiades_T[0:10], pleiades_T_new[0:10]
Explanation: Now let's print the first ten elements of each array to verify that nothing weird happened during this conversion.
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new)
Explanation: Now let's plot these two quantities against one another. There are many ways to plot in pyplot, but I'll use the one that I find to work most consistently and intuitively below.
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new, 'o')
Explanation: Yikes, that's ugly, right? That's because the default plot symbol is a line connecting all the points. In this case what we really want is a so-called scatterplot, which we can do easily by specifying the plotting marker right after the y variable, as below. Below I use 'o', which stands for the circle symbol. For the full list of matplotlib symbols, see this link.
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new, 'go')
Explanation: Note the default plotting color is blue, but you can change this easily by adding a color shorthand before the marker shorthand. Below I use 'g' for green, but here again, there are lots of options, as outlined at this link.
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new, 'go')
ax.set_yscale('log')
Explanation: Colors notwithstanding, this plot is still very ugly and should not look much like an H-R diagram to you. For one thing, H-R diagrams usually have log(Luminosity) on the y-axis, which you do as follows:
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new, 'go')
ax.set_yscale('log')
ax.set_xlim(11000,1000)
Explanation: OK that's much nicer, but should still look backwards to you, because we always draw H-R Diagrams with the Temperature axis running from high to low temperature. This is a pretty easy fix too.
End of explanation
fig,ax = plt.subplots(figsize=(7,7))
ax.plot(pleiades_T_new, pleiades_L_new, 'go')
ax.set_yscale('log')
ax.set_xlim(11000,1000)
plt.title('H-R Diagram for the Pleiades')
plt.xlabel('Temperature (in K)')
plt.ylabel('log(Luminosity (in L$_{\odot}$))')
Explanation: OK! This is starting to look like a good H-R diagram, but without a plot title or axis labels, it's still not very good, so let's add those.
End of explanation
## Add code here to read in the other three files in the Lab 1 directory, and give them descriptive variable names.
## Add code here to identify the column labels for luminosity and temperature. Be careful - columns may not have
## the same names, and be sure to check the units of the quantities.
## Convert to pandas series and data types, if necessary.
## Plot the data in this cell and the following cells for each sample.
Explanation: Exercise 1
Now it's time for you to do some exploring with the rest of the data. For each of the remaining four data files in the Lab 1 directory:   
1) read in the data  
2) find the columns for luminosity and temperature 
(note that some of the raw data has units of log(L) or log(T), which you'll have to "undo" to get the regular units of L and T. Refer to the Homework0 Exercise #1 if you can't remember how)
3) assign the relevant columns to pandas series and convert data types to numeric as necessary   
4) make a plot of the data with appropriate axis labels, ranges, a plot title, etc.
End of explanation
#some fake data
data_x = np.arange(0,100)
data_y = 3*data_x
data_y2 = data_x**2
data_y3 = data_x + 20
data_y4 = np.sqrt(data_x)
# multipanel plot example
fig,((ax1,ax2),(ax3,ax4)) = plt.subplots(2, 2, figsize=(10,10))
fig.suptitle('This is a title for my multipanel plot')
ax1.plot(data_x, data_y, 'go')
ax1.set_title('Figure 1 Title')
ax1.set_xlabel('x label')
ax1.set_ylabel('y label')
ax2.plot(data_x, data_y2, 'bo')
ax2.set_title('Figure 2 Title')
ax2.set_xlabel('x label')
ax2.set_ylabel('y label')
ax3.plot(data_x, data_y3, 'ro')
ax3.set_title('Figure 3 Title')
ax3.set_xlabel('x label')
ax3.set_ylabel('y label')
ax4.plot(data_x, data_y4, 'mo')
ax4.set_title('Figure 4 Title')
ax4.set_xlabel('x label')
ax4.set_ylabel('y label')
#overlay plot example
fig,ax = plt.subplots(figsize=(10,10))
plt.title('This is a title for my multipanel plot')
ax.plot(data_x, data_y, 'go', label='legend entry 1', alpha=0.5)
ax.plot(data_x, data_y2, 'bo', label='legend entry 2', alpha=0.5)
ax.plot(data_x, data_y3, 'ro', label='legend entry 2', alpha=0.5)
ax.plot(data_x, data_y4, 'mo', label='legend entry 2', alpha=0.5)
ax.set_title('Figure Title')
ax.set_xlabel('x label')
ax.set_ylabel('y label')
plt.legend(numpoints=1)
#TRY EXECUTING WITH AND WITHOUT THE FOLLOWING LINE. HERE AND IN THE DATA YOU'LL BE PLOTTING, 
#A SUBJECTIVE DECISION MUST BE MADE ABOUT AXIS RANGES
ax.set_ylim(0,200)
## In this cell, create your own overlay plot showing the different populations. Hint: You may want to plot the 
## sample with the most data points first.
## In this cell, create a multi-panel plot for the different populations.
Explanation: Exercise 2 (Multi-Panel Plots)
The individual plots for each of these clusters are nice, but really we'd like to be able to compare them side-by-side. You can do this by making either a multi-panel plot, or an overlapping plot. A skeleton outline of how to do each is below. Please use this as a framework to make similar plots with our data.
End of explanation
## Your answers to each of the four questions here.
Explanation: Exercise 3 (Comprehension Questions)
1) Describe the differences between the H-R diagrams. Do this both qualitatively (note differences in their appearance) and quantitatively. You might find methods like .min and .max to be useful here.
2) Which of the four groups of stars has stars that are NOT on the main sequence, and which don't? Why, do you think?
3) Why do you think the low temperature (M-dwarf) end of the main sequence cuts off at such a different place in the four samples? Do you think this is a physical effect or an instrumental one and why?
4) Why do you think there are no white dwarfs in any of the samples besides the field sample?
End of explanation
<END_TASK>