文章目录
- 1. Introduction to Seaborn
- 2. Visualizing Two Quantitative Variables
- 2.1 Introduction to relational plots and subplots
- 2.2 Creating subplots with col and row
- 2.3 Creating two-factor subplots
- 2.4 Customizing scatter plots
- 2.5 Changing the size of scatter plot points
- 2.6 Changing the style of scatter plot points
- 2.7 Introduction to line plots
- 2.8 Interpreting line plots
- 2.9 Visualizing standard deviation with line plots
- 2.10 Plotting subgroups in line plots
- 3. Visualizing a Categorical and a Quantitative Variable
- 4. Customizing Seaborn Plots
- 4.1 Changing plot style and color
- 4.2 Changing style and palette
- 4.3 Changing the scale
- 4.4 Using a custom palette
- 4.5 Adding titles and labels: Part 1
- 4.6 FacetGrids vs. AxesSubplots
- 4.8 Adding a title to a FacetGrid object
- 4.8 Adding titles and labels: Part 2
- 4.9 Adding a title and axis labels
- 4.10 Rotating x-tick labels
- 4.11 Putting it all to together
- 4.12 Box plot with subgroups
- 4.13 Bar plot with subgroups and subplots
- 4.14 Well done! What's next?
1. Introduction to Seaborn
1.1 Introduction to Seaborn
1.2 Making a scatter plot with lists
In this exercise, we’ll use a dataset that contains information about 227 countries. This dataset has lots of interesting information on each country, such as the country’s birth rates, death rates, and its gross domestic product (GDP). GDP is the value of all the goods and services produced in a year, expressed as dollars per person.
We’ve created three lists of data from this dataset to get you started. gdp
is a list that contains the value of GDP per country, expressed as dollars per person. phones
is a list of the number of mobile phones per 1,000 people in that country. Finally, percent_literate
is a list that contains the percent of each country’s population that can read and write.
Instruction 1
- Import Matplotlib and Seaborn using the standard naming convention.
- Create a scatter plot of GDP (
gdp
) vs. number of phones per 1000 people (phones
). - Display the plot.
- Change the scatter plot so it displays the percent of the population that can read and write (
percent_literate
) on the y-axis.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create scatter plot with GDP on the x-axis and number of phones on the y-axis
sns.scatterplot (x = gdp, y = phones)
# Show plot
plt.show()
# Change this scatter plot to have percent literate on the y-axis
sns.scatterplot(x = gdp, y = percent_literate)
# Show plot
plt.show()
1.3 Making a count plot with a list
In the last exercise, we explored a dataset that contains information about 227 countries. Let’s do more exploration of this data - specifically, how many countries are in each region of the world?
To do this, we’ll need to use a count plot. Count plots take in a categorical list and return bars that represent the number of list entries per category. You can create one here using a list of regions for each country, which is a variable named region
.
Instruction
- Import Matplotlib and Seaborn using the standard naming conventions.
- Use Seaborn to create a count plot with
region
on the y-axis. - Display the plot.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create count plot with region on the y-axis
sns.countplot(y = region)
# Show plot
plt.show()
1.4 Using pandas with Seaborn
1.5 “Tidy” vs. “untidy” data
Here, we have a sample dataset from a survey of children about their favorite animals. But can we use this dataset as-is with Seaborn? Let’s use Pandas to import the csv file with the data collected from the survey and determine whether it is tidy, which is essential to having it work well with Seaborn.
To get you started, the filepath to the csv file has been assigned to the variable csv_filepath
.
Note that because csv_filepath
is a Python variable, you will not need to put quotation marks around it when you read the csv.
Instruction 1
- Read the csv file located at
csv_filepath
into a DataFrame nameddf
. - Print the head of
df
to show the first five rows.
在这里插入代码片
Instruction 2
View the first five rows of the DataFrame df
. Is it tidy? Why or why not?
□ \square □ Yes, because there are no typos or missing values.
□ \square □ Yes, because it is well organized and easy to read.
■ \blacksquare ■ No, because a single column contains different types of information.
1.6 Making a count plot with a DataFrame
In this exercise, we’ll look at the responses to a survey sent out to young people. Our primary question here is: how many young people surveyed report being scared of spiders? Survey participants were asked to agree or disagree with the statement “I am afraid of spiders”. Responses vary from 1 to 5, where 1 is “Strongly disagree” and 5 is “Strongly agree”.
To get you started, the filepath to the csv file with the survey data has been assigned to the variable csv_filepath
.
Note that because csv_filepath
is a Python variable, you will not need to put quotation marks around it when you read the csv.
Instruction
- Import Matplotlib, Pandas, and Seaborn using the standard names.
- Create a DataFrame named
df
from the csv file located atcsv_filepath
. - Use the
countplot()
function with thex=
anddata=
arguments to create a count plot with the"Spiders"
column values on the x-axis. - Display the plot.
# Import Matplotlib, Pandas, and Seaborn
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Create a DataFrame from csv file
df = pd.read_csv(csv_filepath)
# Create a count plot with "Spiders" on the x-axis
sns.countplot(x = "Spiders", data = df)
# Display the plot
plt.show()
1.7 Adding a third variable with hue
1.8 Hue and scatter plots
In the prior video, we learned how hue
allows us to easily make subgroups within Seaborn plots. Let’s try it out by exploring data from students in secondary school. We have a lot of information about each student like their age, where they live, their study habits and their extracurricular activities.
For now, we’ll look at the relationship between the number of absences they have in school and their final grade in the course, segmented by where the student lives (rural vs. urban area).
Instruction 1
Create a scatter plot with "absences"
on the x-axis and final grade ("G3"
) on the y-axis using the DataFrame student_data
. Color the plot points based on "location"
(urban vs. rural).
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create a scatter plot of absences vs. final grade
sns.scatterplot(x = "absences",
y = "G3",
data = student_data,
hue = "location")
# Show plot
plt.show()
Instruction 2
- Make “Rural” appear before “Urban” in the plot legend.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Change the legend order in the scatter plot
sns.scatterplot(x = "absences",
y = "G3",
data = student_data,
hue = "location",
hue_order = ["Rural", "Urban"])
# Show plot
plt.show()
1.9 Hue and count plots
Let’s continue exploring our dataset from students in secondary school by looking at a new variable. The “school” column indicates the initials of which school the student attended - either “GP” or “MS”.
In the last exercise, we created a scatter plot where the plot points were colored based on whether the student lived in an urban or rural area. How many students live in urban vs. rural areas, and does this vary based on what school the student attends? Let’s make a count plot with subgroups to find out.
Instruction
- Fill in the
palette_colors
dictionary to map the"Rural"
location value to the color"green"
and the"Urban"
location value to the color"blue"
. - Create a count plot with
"school"
on the x-axis using thestudent_data
DataFrame.- Add subgroups to the plot using
"location"
variable and use thepalette_colors
dictionary to make the location subgroups green and blue.
- Add subgroups to the plot using
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create a dictionary mapping subgroup values to colors
palette_colors = {"Rural": "green", "Urban": "blue"}
# Create a count plot of school with location subgroups
sns.countplot(x = "school",
data = student_data,
hue = "location",
palette = palette_colors)
# Display plot
plt.show()
2. Visualizing Two Quantitative Variables
2.1 Introduction to relational plots and subplots
2.2 Creating subplots with col and row
We’ve seen in prior exercises that students with more absences ("absences"
) tend to have lower final grades ("G3"
). Does this relationship hold regardless of how much time students study each week?
To answer this, we’ll look at the relationship between the number of absences that a student has in school and their final grade in the course, creating separate subplots based on each student’s weekly study time ("study_time"
).
Instruction
Modify the code to use relplot()
instead of scatterplot()
.
# Change to use relplot() instead of scatterplot()
sns.relplot(x = "absences",
y = "G3",
kind = "scatter",
data = student_data)
# Show plot
plt.show()
Instruction 2
Modify the code to create one scatter plot for each level of the variable "study_time"
, arranged in columns.
# Change to make subplots based on study time
sns.relplot(x = "absences",
y = "G3",
data = student_data,
kind = "scatter",
col = "study_time")
# Show plot
plt.show()
Instruction 3
Adapt your code to create one scatter plot for each level of a student’s weekly study time, this time arranged in rows.
# Change this scatter plot to arrange the plots in rows instead of columns
sns.relplot(x = "absences",
y = "G3",
data = student_data,
kind = "scatter",
row = "study_time")
# Show plot
plt.show()
2.3 Creating two-factor subplots
Let’s continue looking at the student_data
dataset of students in secondary school. Here, we want to answer the following question: does a student’s first semester grade ("G1"
) tend to correlate with their final grade ("G3"
)?
There are many aspects of a student’s life that could result in a higher or lower final grade in the class. For example, some students receive extra educational support from their school ("schoolsup"
) or from their family ("famsup"
), which could result in higher grades. Let’s try to control for these two factors by creating subplots based on whether the student received extra educational support from their school or family.
Instruction 1
- Use
relplot()
to create a scatter plot with"G1"
on the x-axis and"G3"
on the y-axis, using thestudent_data
DataFrame.
# Create a scatter plot of G1 vs. G3
sns.relplot(x = "G1",
y = "G3",
data = student_data,
kind = "scatter")
# Show plot
plt.show()
Instruction 2
Create column subplots based on whether the student received support from the school ("schoolsup"
), ordered so that “yes” comes before “no”.
# Adjust to add subplots based on school support
sns.relplot(x = "G1",
y = "G3",
data = student_data,
kind = "scatter",
col = "schoolsup",
col_order = ["yes", "no"])
# Show plot
plt.show()
Instruction 3
Add row subplots based on whether the student received support from the family ("famsup"
), ordered so that “yes” comes before “no”. This will result in subplots based on two factors.
# Adjust further to add subplots based on family support
sns.relplot(x = "G1",
y = "G3",
data = student_data,
kind = "scatter",
col = "schoolsup",
col_order = ["yes", "no"],
row = "famsup",
row_order = ["yes", "no"])
# Show plot
plt.show()
2.4 Customizing scatter plots
2.5 Changing the size of scatter plot points
In this exercise, we’ll explore Seaborn’s mpg
dataset, which contains one row per car model and includes information such as the year the car was made, the number of miles per gallon (“M.P.G.”) it achieves, the power of its engine (measured in “horsepower”), and its country of origin.
What is the relationship between the power of a car’s engine ("horsepower"
) and its fuel efficiency ("mpg"
)? And how does this relationship vary by the number of cylinders ("cylinders"
) the car has? Let’s find out.
Let’s continue to use relplot()
instead of scatterplot()
since it offers more flexibility.
Instruction 1
Use relplot()
and the mpg
DataFrame to create a scatter plot with "horsepower"
on the x-axis and "mpg"
on the y-axis. Vary the size of the points by the number of cylinders in the car ("cylinders"
).
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create scatter plot of horsepower vs. mpg
sns.relplot(x = "horsepower",
y = "mpg",
data = mpg,
kind = "scatter",
size = "cylinders")
# Show plot
plt.show()
Introduction 2
To make this plot easier to read, use hue
to vary the color of the points by the number of cylinders in the car ("cylinders"
).
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create scatter plot of horsepower vs. mpg
sns.relplot(x = "horsepower",
y = "mpg",
data = mpg,
kind = "scatter",
hue = "cylinders",
size = "cylinders")
# Show plot
plt.show()
2.6 Changing the style of scatter plot points
Let’s continue exploring Seaborn’s mpg
dataset by looking at the relationship between how fast a car can accelerate ("acceleration"
) and its fuel efficiency ("mpg"
). Do these properties vary by country of origin ("origin"
)?
Note that the "acceleration"
variable is the time to accelerate from 0 to 60 miles per hour, in seconds. Higher values indicate slower acceleration.
Introduction
Use relplot()
and the mpg DataFrame to create a scatter plot with "acceleration"
on the x-axis and "mpg"
on the y-axis. Vary the style and color of the plot points by country of origin ("origin"
).
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create a scatter plot of acceleration vs. mpg
sns.relplot(x = "acceleration",
y = "mpg",
data = mpg,
kind = "scatter",
hue = "origin",
style = "origin")
# Show plot
plt.show()
2.7 Introduction to line plots
2.8 Interpreting line plots
In this exercise, we’ll continue to explore Seaborn’s mpg
dataset, which contains one row per car model and includes information such as the year the car was made, its fuel efficiency (measured in “miles per gallon” or “M.P.G”), and its country of origin (USA, Europe, or Japan).
How has the average miles per gallon achieved by these cars changed over time? Let’s use line plots to find out!
Introduction
Use relplot()
and the mpg
DataFrame to create a line plot with "model_year"
on the x-axis and "mpg"
on the y-axis.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create line plot
sns.relplot(x = "model_year",
y = "mpg",
data = mpg,
kind = "line")
# Show plot
plt.show()
Question
Which of the following is NOT a correct interpretation of this line plot?
□ \square □ The average miles per gallon has generally increased over time.
■ \blacksquare ■ The distribution of miles per gallon is smaller in 1973 compared to 1977.
□ \square □ The 95% confidence interval for average miles per gallon in 1970 is approximately 16 - 19.5 miles per gallon.
□ \square □ This plot assumes that our data is a random sample of all cars in the US, Europe, and Japan.
2.9 Visualizing standard deviation with line plots
In the last exercise, we looked at how the average miles per gallon achieved by cars has changed over time. Now let’s use a line plot to visualize how the distribution of miles per gallon has changed over time.
Instruction
Change the plot so the shaded area shows the standard deviation instead of the confidence interval for the mean.
# Make the shaded area show the standard deviation
sns.relplot(x = "model_year",
y = "mpg",
data = mpg,
kind = "line",
ci = "sd")
# Show plot
plt.show()
2.10 Plotting subgroups in line plots
Let’s continue to look at the mpg dataset. We’ve seen that the average miles per gallon for cars has increased over time, but how has the average horsepower for cars changed over time? And does this trend differ by country of origin?
Instruction 1
Use relplot()
and the mpg
DataFrame to create a line plot with "model_year"
on the x-axis and "horsepower"
on the y-axis. Turn off the confidence intervals on the plot.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Create line plot of model year vs. horsepower
sns.relplot(x = "model_year",
y = "horsepower",
data = mpg,
kind = "line",
ci = None)
# Show plot
plt.show()
Instruction 2
Create different lines for each country of origin ("origin"
) that vary in both line style and color.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Change to create subgroups for country of origin
sns.relplot(x = "model_year",
y = "horsepower",
data = mpg,
kind = "line",
ci = None,
hue = "origin",
style = "origin")
# Show plot
plt.show()
Instruction 3
- Add markers for each data point to the lines.
- Use the
dashes
parameter to use solid lines for all countries, while still allowing for different marker styles for each line.
# Import Matplotlib and Seaborn
import matplotlib.pyplot as plt
import seaborn as sns
# Add markers and make each line have the same style
sns.relplot(x = "model_year",
y = "horsepower",
data = mpg,
kind = "line",
ci = None,
style = "origin",
hue = "origin",
markers = True,
dashes = False)
# Show plot
plt.show()
3. Visualizing a Categorical and a Quantitative Variable
3.1 Count plots and bar plots
3.2 Count plots
In 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"
).
Instruction 1
Use sns.catplot()
to create a count plot using the survey_data
DataFrame with "Internet usage"
on the x-axis.
# Create count plot of internet usage
sns.catplot(x = "Internet usage",
data = survey_data,
kind = "count")
# Show plot
plt.show()
Instruction 2
Make the bars horizontal instead of vertical.
# Change the orientation of the plot
sns.catplot(y = "Internet usage",
data = survey_data,
kind = "count")
# Show plot
plt.show()
Instruction 3
Create column subplots based on "Age Category"
, which separates respondents into those that are younger than 21 vs. 21 and older.
# Create column subplots based on age category
sns.catplot(y = "Internet usage",
data = survey_data,
kind = "count",
col = "Age Category")
# Show plot
plt.show()
3.3 Bar plots with percentages
Let’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”).
Instruction
Use the survey_data
DataFrame and sns.catplot()
to create a bar plot with "Gender"
on the x-axis and "Interested in Math"
on the y-axis.
# 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()
When the y-variable is True/False, bar plots will show the percentage of responses reporting True. This plot shows us that males report a much higher interest in math compared to females.
3.4 Customizing bar plots
In 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: "<2 hours"
, "2 to 5 hours"
, "5 to 10 hours"
, or ">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.
Instruction 1
Use sns.catplot()
to create a bar plot with "study_time"
on the x-axis and final grade ("G3"
) on the y-axis, using the student_data
DataFrame.
# 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()
Instruction 2
Using the order
parameter, rearrange the categories so that they are in order from lowest study time to highest.
# 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()
Instruction 3
Update the plot so that it no longer displays confidence intervals.
# 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()
3.5 Box plots
3.6 Create and interpret a box plot
Let’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"
).
Instruction
Use sns.catplot()
and the student_data
DataFrame to create a box plot with "study_time"
on the x-axis and "G3"
on the y-axis. Set the ordering of the categories to study_time_order
.
# 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()
Question
Which of the following is a correct interpretation of this box plot?
□ \square □ The 75th percentile of grades is highest among students who study more than 10 hours a week.
□ \square □ There are no outliers plotted for these box plots.
□ \square □ The 5th percentile of grades among students studying less than 2 hours is 5.0.
■ \blacksquare ■ The median grade among students studying less than 2 hours is 10.0.
3.7 Omitting outliers
Now 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.
As a reminder, you can omit outliers in box plots by setting the sym
parameter equal to an empty string (""
).
Instruction
- Use
sns.catplot()
to create a box plot with thestudent_data
DataFrame, putting"internet"
on the x-axis and"G3"
on the y-axis. - Add subgroups so each box plot is colored based on
"location"
. - Do not display the outliers.
# 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()
3.8 Adjusting the whiskers
In 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.
Instruction 1
Adjust the code to make the box plot whiskers to extend to 0.5 * IQR. Recall: the IQR is the interquartile range.
# Set the whiskers to 0.5 * IQR
sns.catplot(x = "romantic",
y = "G3",
data = student_data,
kind = "box",
whis = [5, 95])
# Show plot
plt.show()
Instruction 2
Change the code to set the whiskers to extend to the 5th and 95th percentiles.
# 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()
Instruction 3
Change the code to set the whiskers to extend to the min and max values.
# 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()
3.9 Point plots
3.10 Customizing point plots
Let’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"
).
Instruction 1
Use sns.catplot()
and the student_data
DataFrame to create a point plot with "famrel"
on the x-axis and number of absences ("absences"
) on the y-axis.
# Create a point plot of family relationship vs. absences
sns.catplot(x = "famrel",
y = "absences",
data = student_data,
kind = "point")
# Show plot
plt.show()
Instruction 2
Add “caps” to the end of the confidence intervals with size 0.2
.
# Add caps to the confidence interval
sns.catplot(x = "famrel",
y = "absences",
data = student_data,
kind = "point",
capsize = 0.2)
# Show plot
plt.show()
Instruction 3
Remove the lines joining the points in each category.
# 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()
While the average number of absences is slightly smaller among students with higher-quality family relationships, the large confidence intervals tell us that we can’t be sure there is an actual association here.
3.11 Point plots with subgroups
Let’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.
Instruction 1
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"
)
# Create a point plot with subgroups
sns.catplot(x = "romantic",
y = "absences",
data = student_data,
kind = "point",
hue = "school")
# Show plot
plt.show()
Instruction 2
Turn off the confidence intervals for the plot.
# 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()
Instruction 3
Since there may be outliers of students with many absences, import the median
function from numpy
and display the median number of absences instead of the average.
# 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()
It looks like students in romantic relationships have a higher average and median number of absences in the GP school, but this association does not hold for the MS school.
4. Customizing Seaborn Plots
4.1 Changing plot style and color
4.2 Changing style and palette
Let’s return to our dataset containing the results of a survey given to young people about their habits and preferences. We’ve provided the code to create a count plot of their responses to the question “How often do you listen to your parents’ advice?”. Now let’s change the style and palette to make this plot easier to interpret.
Instruction 1
Set the style to "whitegrid"
to help the audience determine the number of responses in each category.
# Set the style to "whitegrid"
sns.set_style("whitegrid")
# Create a count plot of survey responses
category_order = ["Never",
"Rarely",
"Sometimes",
"Often",
"Always"]
sns.catplot(x = "Parents Advice",
data = survey_data,
kind = "count",
order = category_order)
# Show plot
plt.show()
Instruction 2
Set the color palette to the sequential palette named “Purples”.
# Set the color palette to "Purples"
sns.set_style("whitegrid")
# Create a count plot of survey responses
category_order = ["Never",
"Rarely",
"Sometimes",
"Often",
"Always"]
sns.catplot(x = "Parents Advice",
data = survey_data,
kind = "count",
order = category_order,
palette = "Purples")
# Show plot
plt.show()
Instruction 3
Change the color palette to the diverging palette named "RdBu"
.
# Change the color palette to "RdBu"
sns.set_style("whitegrid")
sns.set_palette("RdBu")
# Create a count plot of survey responses
category_order = ["Never",
"Rarely",
"Sometimes",
"Often",
"Always"]
sns.catplot(x = "Parents Advice",
data = survey_data,
kind = "count",
order = category_order)
# Show plot
plt.show()
4.3 Changing the scale
In this exercise, we’ll continue to look at the dataset containing responses from a survey of young people. Does the percentage of people reporting that they feel lonely vary depending on how many siblings they have? Let’s find out using a bar plot, while also exploring Seaborn’s four different plot scales (“contexts”).
Instruction 1
Set the scale (“context”) to "paper"
, which is the smallest of the scale options.
# Set the context to "paper"
sns.set_context("paper")
# Create bar plot
sns.catplot(x = "Number of Siblings",
y = "Feels Lonely",
data = survey_data,
kind = "bar")
# Show plot
plt.show()
Instruction 2
Change the context to "notebook"
to increase the scale.
# Change the context to "notebook"
sns.set_context("notebook")
# Create bar plot
sns.catplot(x = "Number of Siblings",
y = "Feels Lonely",
data = survey_data,
kind = "bar")
# Show plot
plt.show()
Instruction 3
Change the context to "talk"
to increase the scale.
# Change the context to "talk"
sns.set_context("talk")
# Create bar plot
sns.catplot(x = "Number of Siblings",
y = "Feels Lonely",
data = survey_data,
kind = "bar")
# Show plot
plt.show()
Instruction 4
Change the context to "poster"
, which is the largest scale available.
# Change the context to "poster"
sns.set_context("poster")
# Create bar plot
sns.catplot(x = "Number of Siblings",
y = "Feels Lonely",
data = survey_data,
kind = "bar")
# Show plot
plt.show()
4.4 Using a custom palette
So far, we’ve looked at several things in the dataset of survey responses from young people, including their internet usage, how often they listen to their parents, and how many of them report feeling lonely. However, one thing we haven’t done is a basic summary of the type of people answering this survey, including their age and gender. Providing these basic summaries is always a good practice when dealing with an unfamiliar dataset.
The code provided will create a box plot showing the distribution of ages for male versus female respondents. Let’s adjust the code to customize the appearance, this time using a custom color palette.
Instruction
- Set the style to
"darkgrid"
. - Set a custom color palette with the hex color codes
"#39A7D0"
and"#36ADA4"
.
# Set the style to "darkgrid"
sns.set_style("darkgrid")
# Set a custom color palette
sns.set_palette(["#39A7D0", "#36ADA4"])
# Create the box plot of age distribution by gender
sns.catplot(x = "Gender",
y = "Age",
data = survey_data,
kind = "box")
# Show plot
plt.show()
4.5 Adding titles and labels: Part 1
4.6 FacetGrids vs. AxesSubplots
In the recent lesson, we learned that Seaborn plot functions create two different types of objects: FacetGrid
objects and AxesSubplot
objects. The method for adding a title to your plot will differ depending on the type of object it is.
In the code provided, we’ve used relplot()
with the miles per gallon dataset to create a scatter plot showing the relationship between a car’s weight and its horsepower. This scatter plot is assigned to the variable name g
. Let’s identify which type of object it is.
Instruction
Identify what type of object plot g
is and assign it to the variable type_of_g
.
# Create scatter plot
g = sns.relplot(x = "weight",
y = "horsepower",
data = mpg,
kind = "scatter")
# Identify plot type
type_of_g = type(g)
# Print type
print(type_of_g)
Question
We’ve just seen that sns.relplot()
creates FacetGrid objects. Which other Seaborn function creates a FacetGrid object instead of an AxesSubplot object?
■ \blacksquare ■ sns.catplot()
□ \square □ sns.scatterplot()
□ \square □ sns.boxplot()
□ \square □ sns.countplot()
catplot()
supports creating subplots, so it creates a FacetGrid
object.
4.8 Adding a title to a FacetGrid object
In the previous exercise, we used relplot() with the miles per gallon dataset to create a scatter plot showing the relationship between a car’s weight and its horsepower. This created a FacetGrid object. Now that we know what type of object it is, let’s add a title to this plot.
Instruction
Add the following title to this plot: "Car Weight vs. Horsepower"
.
# Create scatter plot
g = sns.relplot(x = "weight",
y = "horsepower",
data = mpg,
kind = "scatter")
# Add a title "Car Weight vs. Horsepower"
g.fig.suptitle("Car Weight vs. Horsepower")
# Show plot
plt.show()
4.8 Adding titles and labels: Part 2
4.9 Adding a title and axis labels
Let’s continue to look at the miles per gallon dataset. This time we’ll create a line plot to answer the question: How does the average miles per gallon achieved by cars change over time for each of the three places of origin? To improve the readability of this plot, we’ll add a title and more informative axis labels.
In the code provided, we create the line plot using the lineplot()
function. Note that lineplot()
does not support the creation of subplots, so it returns an AxesSubplot
object instead of an FacetGrid
object.
Instruction 1
Add the following title to the plot: "Average MPG Over Time"
.
# Create line plot
g = sns.lineplot(x = "model_year",
y = "mpg_mean",
data = mpg_mean,
hue = "origin")
# Add a title "Average MPG Over Time"
g.set_title("Average MPG Over Time")
# Show plot
plt.show()
Instruction 2
Label the x-axis as “Car Model Year” and the y-axis as “Average MPG”.
# Create line plot
g = sns.lineplot(x = "model_year",
y = "mpg_mean",
data = mpg_mean,
hue = "origin")
# Add a title "Average MPG Over Time"
g.set_title("Average MPG Over Time")
# Add x-axis and y-axis labels
g.set(xlabel = "Car Model Year",
ylabel = "Average MPG")
# Show plot
plt.show()
4.10 Rotating x-tick labels
In this exercise, we’ll continue looking at the miles per gallon dataset. In the code provided, we create a point plot that displays the average acceleration for cars in each of the three places of origin. Note that the "acceleration"
variable is the time to accelerate from 0 to 60 miles per hour, in seconds. Higher values indicate slower acceleration.
Let’s use this plot to practice rotating the x-tick labels. Recall that the function to rotate x-tick labels is a standalone Matplotlib function and not a function applied to the plot object itself.
Instruction
Rotate the x-tick labels 90 degrees.
# Create point plot
sns.catplot(x = "origin",
y = "acceleration",
data = mpg,
kind = "point",
join = False,
capsize = 0.1)
# Rotate x-tick labels
plt.xticks(rotation = 90)
# Show plot
plt.show()
4.11 Putting it all to together
4.12 Box plot with subgroups
In this exercise, we’ll look at the dataset containing responses from a survey given to young people. One of the questions asked of the young people was: “Are you interested in having pets?” Let’s explore whether the distribution of ages of those answering “yes” tends to be higher or lower than those answering “no”, controlling for gender.
Instruction
- Set the color palette to
"Blues"
. - Add subgroups to color the box plots based on
"Interested in Pets"
. - Set the title of the
FacetGrid
objectg
to “Age of Those Interested in Pets vs. Not”. - Make the plot display using a Matplotlib function.
# Set palette to "Blues"
sns.set_palette("Blues")
# Adjust to add subgroups based on "Interested in Pets"
g = sns.catplot(x = "Gender",
y = "Age",
data = survey_data,
kind = "box",
hue = "Interested in Pets")
# Set title to "Age of Those Interested in Pets vs. Not"
g.fig.suptitle("Age of Those Interested in Pets vs. Not")
# Show plot
plt.show()
4.13 Bar plot with subgroups and subplots
In this exercise, we’ll return to our young people survey dataset and investigate whether the proportion of people who like techno music ("Likes Techno"
) varies by their gender ("Gender"
) or where they live ("Village - town"
). This exercise will give us an opportunity to practice the many things we’ve learned throughout this course!
Instruction
- Set the figure style to
"dark"
. - Adjust the bar plot code to add subplots based on
"Gender"
, arranged in columns. - Add the title “Percentage of Young People Who Like Techno” to this
FacetGrid
plot. - Label the x-axis “Location of Residence” and y-axis “% Who Like Techno”.
# Set the figure style to "dark"
sns.set_style("dark")
# Adjust to add subplots per gender
g = sns.catplot(x = "Village - town",
y = "Likes Techno",
data = survey_data,
kind = "bar",
hue = "Gender",
col = "Gender")
# Add title and axis labels
g.fig.suptitle("Percentage of Young People Who Like Techno",
y = 1.02)
g.set(xlabel = "Location of Residence",
ylabel = "% Who Like Techno")
# Show plot
plt.show()