Introduction to Seaborn
What is Seaborn, and when should you use it? In this chapter, you will find out! Plus, you will learn how to create scatter plots and count plots with both lists of data and pandas DataFrames. You will also be introduced to one of the big advantages of using Seaborn - the ability to easily add a third variable to your plots by using color to represent different subgroups. This is the Summary of lecture "Introduction to Data Visualization with Seaborn", via datacamp.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['figure.figsize'] = (10, 5)
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.
df = pd.read_csv('./dataset/countries-of-the-world.csv')
df.head()
gdp = df['GDP ($ per capita)'].tolist()
phones = df['Phones (per 1000)'].tolist()
percent_literate = df['Literacy (%)'].tolist()
region = df['Region'].tolist()
sns.scatterplot(x=gdp, y=phones);
sns.scatterplot(x=gdp, y=percent_literate);
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
.
sns.countplot(y=region);
Sub-Saharan Africa contains the most countries in this list. We'll revisit count plots later in the course.
"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.
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".
df = pd.read_csv('./dataset/young-people-survey-responses.csv')
# Create a count plot with "Spiders" on the x-axis
sns.countplot('Spiders', data=df);
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).
student_data = pd.read_csv('./dataset/student-alcohol-consumption.csv', index_col=0)
student_data.head()
sns.scatterplot(x="absences", y="G3", data=student_data, hue='location');
sns.scatterplot(x="absences", y="G3",
data=student_data,
hue='location',
hue_order=['Rural', 'Urban']);
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.
palette_colors = {'Rural': "green", 'Urban': "blue"}
# Create a count plot of school with location subgroups
sns.countplot('school', data=student_data, hue='location' ,palette=palette_colors);
Students at GP tend to come from an urban location, but students at MS are more evenly split.