<jupyter_start><jupyter_text>Read datasets<jupyter_code>import pandas as pd 
countries_of_the_world = pd.read_csv('../datasets/countries-of-the-world.csv')
countries_of_the_world.head()
mpg = pd.read_csv('../datasets/mpg.csv')
mpg.head()
student_data = pd.read_csv('../datasets/student-alcohol-consumption.csv')
student_data.head()
young_people_survey_data = pd.read_csv('../datasets/young-people-survey-responses.csv')
young_people_survey_data.head()
import matplotlib.pyplot as plt 
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Count plotsIn this exercise, we'll return to exploring our dataset that contains the responses to a survey sent out to young people. We might suspect that young people spend a lot of time on the internet, but how much do they report using the internet each day? Let's use a count plot to break down the number of survey responses in each category and then explore whether it changes based on age.As a reminder, to create a count plot, we'll use the catplot() function and specify the name of the categorical variable to count (x=____), the Pandas DataFrame to use (data=____), and the type of plot (kind="count").Seaborn has been imported as sns and matplotlib.pyplot has been imported as plt.<jupyter_code>survey_data = young_people_survey_data
# Create count plot of internet usage
sns.catplot(x="Internet usage", data=survey_data, kind="count")

# Show plot
plt.show()
# Change the orientation of the plot
sns.catplot(y="Internet usage", data=survey_data,
            kind="count")

# Show plot
plt.show()
survey_data["Age Category"] = ['Less than 21' if x < 21 else '21+' for x in survey_data['Age']]

# Create column subplots based on age category
sns.catplot(y="Internet usage", 
            data=survey_data,
            kind="count", 
            col="Age Category")
# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Bar plots with percentagesLet's continue exploring the responses to a survey sent out to young people. The variable "Interested in Math" is True if the person reported being interested or very interested in mathematics, and False otherwise. What percentage of young people report being interested in math, and does this vary based on gender? Let's use a bar plot to find out.As a reminder, we'll create a bar plot using the catplot() function, providing the name of categorical variable to put on the x-axis (x=____), the name of the quantitative variable to summarize on the y-axis (y=____), the Pandas DataFrame to use (data=____), and the type of categorical plot (kind="bar").Seaborn has been imported as sns and matplotlib.pyplot has been imported as plt.<jupyter_code>survey_data["Interested in Math"] = [True if x > 3 else False for x in survey_data['Mathematics']]
# Create a bar plot of interest in math, separated by gender
sns.catplot(x="Gender",
            y="Interested in Math",
            data=survey_data,
            kind="bar")
# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Customizing bar plotsIn this exercise, we'll explore data from students in secondary school. The "study_time" variable records each student's reported weekly study time as one of the following categories: "10 hours". Do students who report higher amounts of studying tend to get better final grades? Let's compare the average final grade among students in each category using a bar plot.Seaborn has been imported as sns and matplotlib.pyplot has been imported as plt.<jupyter_code># Create bar plot of average final grade in each study category
sns.catplot(x="study_time",
            y="G3",
            data=student_data,
            kind="bar")

# Show plot
plt.show()
# Rearrange the categories
sns.catplot(x="study_time", y="G3",
            data=student_data,
            kind="bar",
             order=["<2 hours",
                   "2 to 5 hours",
                   "5 to 10 hours",
                   ">10 hours"])

# Show plot
plt.show()
# Turn off the confidence intervals
sns.catplot(x="study_time", y="G3",
            data=student_data,
            kind="bar",
            order=["<2 hours", 
                   "2 to 5 hours", 
                   "5 to 10 hours", 
                   ">10 hours"], 
            ci=None)
# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Create and interpret a box plotLet's continue using the student_data dataset. In an earlier exercise, we explored the relationship between studying and final grade by using a bar plot to compare the average final grade ("G3") among students in different categories of "study_time".In this exercise, we'll try using a box plot look at this relationship instead. As a reminder, to create a box plot you'll need to use the catplot() function and specify the name of the categorical variable to put on the x-axis (x=____), the name of the quantitative variable to summarize on the y-axis (y=____), the Pandas DataFrame to use (data=____), and the type of plot (kind="box").We have already imported matplotlib.pyplot as plt and seaborn as sns.<jupyter_code># Specify the category ordering
study_time_order = ["<2 hours", "2 to 5 hours", 
                    "5 to 10 hours", ">10 hours"]

# Create a box plot and set the order of the categories
sns.catplot(x="study_time",
            y="G3", 
            data=student_data, 
            kind='box', 
            order=study_time_order)

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>QuestionWhich of the following is a correct interpretation of this box plot?Possible Answers: The median grade among students studying less than 2 hours is 10.0.  Omitting outliersNow let's use the student_data dataset to compare the distribution of final grades ("G3") between students who have internet access at home and those who don't. To do this, we'll use the "internet" variable, which is a binary (yes/no) indicator of whether the student has internet access at home.Since internet may be less accessible in rural areas, we'll add subgroups based on where the student lives. For this, we can use the "location" variable, which is an indicator of whether a student lives in an urban ("Urban") or rural ("Rural") location.Seaborn has already been imported as sns and matplotlib.pyplot has been imported as plt. As a reminder, you can omit outliers in box plots by setting the sym parameter equal to an empty string ("").<jupyter_code># Create a box plot with subgroups and omit the outliers
sns.catplot(x="internet", 
            y="G3", 
            data=student_data, 
            kind='box', 
            hue="location", 
            sym="")

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Adjusting the whiskersIn the lesson we saw that there are multiple ways to define the whiskers in a box plot. In this set of exercises, we'll continue to use the student_data dataset to compare the distribution of final grades ("G3") between students who are in a romantic relationship and those that are not. We'll use the "romantic" variable, which is a yes/no indicator of whether the student is in a romantic relationship.Let's create a box plot to look at this relationship and try different ways to define the whiskers.We've already imported Seaborn as sns and matplotlib.pyplot as plt.<jupyter_code># Extend the whiskers to the 5th and 95th percentile
sns.catplot(x="romantic", y="G3",
            data=student_data,
            kind="box",
            whis=0.5)

# Show plot
plt.show()
# Extend the whiskers to the 5th and 95th percentile
sns.catplot(x="romantic", y="G3",
            data=student_data,
            kind="box",
            whis=[5, 95])

# Show plot
plt.show()
# Set the whiskers at the min and max values
sns.catplot(x="romantic", y="G3",
            data=student_data,
            kind="box",
            whis=[0, 100])

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Customizing point plotsLet's continue to look at data from students in secondary school, this time using a point plot to answer the question: does the quality of the student's family relationship influence the number of absences the student has in school? Here, we'll use the "famrel" variable, which describes the quality of a student's family relationship from 1 (very bad) to 5 (very good).As a reminder, to create a point plot, use the catplot() function and specify the name of the categorical variable to put on the x-axis (x=____), the name of the quantitative variable to summarize on the y-axis (y=____), the Pandas DataFrame to use (data=____), and the type of categorical plot (kind="point").We've already imported Seaborn as sns and matplotlib.pyplot as plt.<jupyter_code># Create a point plot of family relationship vs. absences
sns.catplot(x="famrel", y="absences",
			data=student_data,
            kind="point")
           
# Show plot
plt.show()
# Add caps to the confidence interval
sns.catplot(x="famrel", y="absences",
			data=student_data,
            kind="point",
            capsize=0.2)
        
# Show plot
plt.show()
# Remove the lines joining the points
sns.catplot(x="famrel", y="absences",
			data=student_data,
            kind="point",
            capsize=0.2,
            join=False)
            
# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Point plots with subgroupsLet's continue exploring the dataset of students in secondary school. This time, we'll ask the question: is being in a romantic relationship associated with higher or lower school attendance? And does this association differ by which school the students attend? Let's find out using a point plot.We've already imported Seaborn as sns and matplotlib.pyplot as plt. Use sns.catplot() and the student_data DataFrame to create a point plot with relationship status ("romantic") on the x-axis and number of absences ("absences") on the y-axis. Create subgroups based on the school that they attend ("school")<jupyter_code># Create a point plot with subgroups
sns.catplot(x="romantic",
            y="absences",
            data=student_data,
            kind="point",
            hue="school")

# Show plot
plt.show()
# Turn off the confidence intervals for this plot
sns.catplot(x="romantic", y="absences",
			data=student_data,
            kind="point",
            hue="school",
            ci=None)

# Show plot
plt.show()
# Import median function from numpy
from numpy import median

# Plot the median number of absences instead of the mean
sns.catplot(x="romantic", y="absences",
			data=student_data,
            kind="point",
            hue="school",
            ci=None, estimator=median)

# Show plot
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0843418707195797, 'fraction_numerical': 0.00647340275823248, 'mean_word_length': 3.659090909090909, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '13024', 'license': 'MIT', 'n_tokens_mistral': 3093, 'n_tokens_neox': 2927, 'n_words': 1283, 'path': 'introduction-to-data-visualization-with-seaborn/3. Visualizing a Categorical and a Quantitative Variable/notebook_section_3.ipynb', 'repo_name': 'nhutnamhcmus/datacamp-playground', 'size': '10659'}