<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python Plotting Challenge
Goals of this Challenge
Students will test their ability to
Step1:  Before we do anything, we should load the data
Step2:  Descriptives
Let's first use some basic histograms to visualize our variables. Let's do this for PWB, Locomotion, Assessment, Exercise, and AcademAchieve.
Step3:  Scatterplot
Let's look at how locomotion and assessment each relate to psychological well-being (PWB)
First, make 2 scatter plots in with locomotion score/assessment score (respectively) on the x axis and PWB on the y axis.
Step4:  Does it look like there's a relationship? Let's test this further by fitting a line to these points, with a 95% confidence interval.
Step5:  Hmm, 'PWB' and 'Locomotion/Assessment' aren't very descriptive axis titles...can you make them something more helpful, and also title the plots?
(Hint
Step6:  Categorical plots
Okay, so let’s just think about locomotion for now. Maybe there’s a difference in locomoation as a function of gender. Plot a bar plot showing the average locomotion score and 95% CI for each gender.
Remember, you might want to recode the variable to get your plot to show descriptive factor levels!
Step7:  Great! Sometimes bar plots can hide the distribution of the raw data though. Let’s plot just the raw datapoints broken down by gender
Step8:  What if we wanted to put both of these plots on top of each other? Try using the individual functions for these catplots to create a single graph. Hint
Step9:  Okay, so while there might not be huge gender difference in this dataset on locomotion, maybe locomotion predicts different things in males and females.
Make a scatter plot + linear regression line (with confidence interval) with locomotion on the x axis and PWB on the y axis, but this time break this relationship out by gender
Step10:  Okay, let’s switch it up and look at how PWB predicts academic achievement broken down by gender
Step11:  Okay, now let’s say we want to make this one look nice! Change the style, font, colors, and labels
<ASSISTANT_TASK:>
Python Code:
# load packages we will be using for this challenge
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
sns.set(rc={'figure.figsize':(12,8)})
Explanation: Python Plotting Challenge
Goals of this Challenge
Students will test their ability to:
Generate beautiful statistical visualizations in Python using the seaborn package
In this challenge, you'll get the opportunity to plot data from paper by Garcia and colleagues (2015). In it, there is data from 116 Swedish high school students.
For the purposes of this data challenge, we'll be looking at how regulatory mode -- as represented by the locomotion and assessment variables -- predicts psychological well-being and academic achievement.
gender: Whether students were coded as female (gender==1) or male (gender==2)
age: Age in years.
PWB: This is a "Psychological Well-Being" scale. Higher scores (on a scale of 0-6) indicated higher psychological well-being
Locomotion:   This measures a form of "regulatory mode," and is a personality measure related to goal-directed behavior. High-locomotion people favor taking action over waiting for opportunities to present themselves.
Assessment: This is also a form of "regulatory mode." High-assessment people place a high value on close observation, data collection, and critical thinking. Importantly, locomotion and assessment are designed to be uncorrelated, so someone can have low scores in both (and be hesitant and inattentive) or high scores in both (and be prone to action and hypervigilant).
Excercise:    This is a composite measure of exercise behavior. Higher scores denote a more active student overall.
AcademAchieve: This is a measure of academic achievement using the 20-point scale that is thenorm in Sweden. Thus, this can be thought of as analogous to a GPA.
End of explanation
garcia = pd.read_csv("garcia.csv")
garcia.head()
Explanation: Before we do anything, we should load the data:
End of explanation
sns.distplot(garcia["PWB"]);
sns.distplot(garcia["Locomotion"]);
Explanation: Descriptives
Let's first use some basic histograms to visualize our variables. Let's do this for PWB, Locomotion, Assessment, Exercise, and AcademAchieve.
End of explanation
sns.scatterplot(x="Locomotion",y="PWB",data=garcia);
sns.scatterplot(x="Assessment",y="PWB",data=garcia);
Explanation: Scatterplot
Let's look at how locomotion and assessment each relate to psychological well-being (PWB)
First, make 2 scatter plots in with locomotion score/assessment score (respectively) on the x axis and PWB on the y axis.
End of explanation
lPlot = sns.lmplot(x="Locomotion",y="PWB",data=garcia,truncate=True);
aPlot = sns.lmplot(x="Assessment",y="PWB",data=garcia,truncate=True);
Explanation: Does it look like there's a relationship? Let's test this further by fitting a line to these points, with a 95% confidence interval.
End of explanation
lPlot = sns.lmplot(x="Locomotion",y="PWB",data=garcia,truncate=True)
ax = plt.gca()
ax.set_title("Assessment")
ax.set_ylabel("Psychological Well Being")
ax.set_xlabel("Assessment Focus");
Explanation: Hmm, 'PWB' and 'Locomotion/Assessment' aren't very descriptive axis titles...can you make them something more helpful, and also title the plots?
(Hint: lmplot works a little differently from scatterplot. In scatterplot you can call set_title() directly on your plot object, but for lmplot you need to get the axis that is currently being plotted to using the function plt.gca() and then call set_title() on that)
End of explanation
garcia["gender"] = garcia["gender"].replace({1:'Female',2:'Male'})
sns.catplot(x="gender",y="Locomotion",data=garcia,kind="bar");
Explanation: Categorical plots
Okay, so let’s just think about locomotion for now. Maybe there’s a difference in locomoation as a function of gender. Plot a bar plot showing the average locomotion score and 95% CI for each gender.
Remember, you might want to recode the variable to get your plot to show descriptive factor levels!
End of explanation
sns.catplot(x="gender",y="Locomotion",data=garcia,kind="strip");
Explanation: Great! Sometimes bar plots can hide the distribution of the raw data though. Let’s plot just the raw datapoints broken down by gender:
End of explanation
sns.barplot(x="gender",y="Locomotion",data=garcia,alpha=0.5)
sns.stripplot(x="gender",y="Locomotion",data=garcia);
Explanation: What if we wanted to put both of these plots on top of each other? Try using the individual functions for these catplots to create a single graph. Hint: making the bars slightly lighter than the points helps to make all of the points visible.
End of explanation
sns.lmplot(x="Locomotion",y="PWB",data=garcia,height=8,truncate=True,col="gender",col_wrap=2);
Explanation: Okay, so while there might not be huge gender difference in this dataset on locomotion, maybe locomotion predicts different things in males and females.
Make a scatter plot + linear regression line (with confidence interval) with locomotion on the x axis and PWB on the y axis, but this time break this relationship out by gender
End of explanation
sns.lmplot(x="PWB",y="AcademAchieve",data=garcia,height=8,truncate=True,col="gender",col_wrap=2);
Explanation: Okay, let’s switch it up and look at how PWB predicts academic achievement broken down by gender:
End of explanation
sns.set(style="ticks",font="Arial",palette="Pastel1",font_scale=2)
g = sns.lmplot(x="PWB",y="AcademAchieve",data=garcia,height=8,truncate=True,col="gender",col_wrap=2)
for i in range(2):
    g.axes[i].set_xlabel('Scale of Psychological Well-Being (1-6)')
    g.axes[i].set_ylabel('Academic Achievement (0-20)')
Explanation: Okay, now let’s say we want to make this one look nice! Change the style, font, colors, and labels:
End of explanation
<END_TASK>