<jupyter_start><jupyter_text>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.**<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns<jupyter_output><empty_output><jupyter_text>Making a scatter plot with listsIn 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.Make three lists of data from the dataset `countries_of_the_world`. `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.<jupyter_code>countries_of_the_world = pd.read_csv('countries_of_the_world.csv')
gdp = [700.0, 4500.0, 6000.0, 8000.0, 19000.0, 1900.0, 8600.0, 11000.0, 11200.0, 3500.0, 28000.0, 29000.0, 30000.0, 3400.0, 16700.0, 16900.0, 1900.0, 15700.0, 6100.0, 29100.0, 4900.0, 1100.0, 36000.0, 1300.0, 2400.0, 6100.0, 9000.0, 7600.0, 16000.0, 18600.0, 7600.0, 1100.0, 1800.0, 600.0, 1900.0, 1800.0, 29800.0, 1400.0, 35000.0, 1100.0, 1200.0, 9900.0, 5000.0, 6300.0, 700.0, 700.0, 700.0, 5000.0, 9100.0, 1400.0, 10600.0, 2900.0, 19200.0, 15700.0, 31100.0, 1300.0, 5400.0, 6000.0, 500.0, 3300.0, 4000.0, 4800.0, 2700.0, 700.0, 12300.0, 700.0, 22000.0, 5800.0, 27400.0, 27600.0, 8300.0, 17500.0, 5500.0, 1700.0, 600.0, 2500.0, 27600.0, 2200.0, 17500.0, 20000.0, 20000.0, 5000.0, 8000.0, 21000.0, 4100.0, 20000.0, 2100.0, 800.0, 4000.0, 1600.0, 2600.0, 28800.0, 13900.0, 30900.0, 2900.0, 3200.0, 7000.0, 1500.0, 29600.0, 21000.0, 19800.0, 26700.0, 3900.0, 28200.0, 24800.0, 4300.0, 6300.0, 1000.0, 800.0, 1300.0, 17800.0, 19000.0, 1600.0, 1700.0, 10200.0, 4800.0, 3000.0, 1000.0, 6400.0, 25000.0, 11400.0, 55100.0, 19400.0, 67000, 800.0, 600.0, 9000.0, 3900.0, 900.0, 17700.0, 1600.0, 14400.0, 1800.0, 11400.0, 2600.0, 9000.0, 2000.0, 1800.0, 27000.0, 1800.0, 3400.0, 4000.0, 1200.0, 7200.0, 5000.0,1400.0, 28600.0, 11400.0, 15000.0, 21600.0, 2300.0, 800.0, 900.0, 2500.0, 37800.0, 13100.0, 2100.0, 9000.0, 6300.0, 2200.0, 4700.0, 5100.0, 4600.0, 11100.0, 18000.0, 16800.0, 21500.0, 5800.0, 7000.0, 8900.0, 1300.0, 2500.0, 8800.0, 5400.0, 6900.0, 2900.0, 5600.0, 34600.0, 1200.0, 11800.0, 1600.0, 2200.0, 7800.0, 500.0, 3700.0, 13300.0, 19000.0, 1700.0, 500.0, 10700.0, 22000.0, 3700.0, 1900.0, 4000.0, 4900.0, 26800.0, 32700.0, 3300.0, 23400.0, 1000.0, 600.0, 7400.0, 1500.0, 2200.0, 9500.0, 6900.0, 6700.0, 5800.0, 9600.0, 1100.0, 1400.0, 5400.0, 23200.0, 27700.0, 37800.0, 2800.0, 1700.0, 2900.0, 4800.0, 2500.0, 17200.0, 3700.0, 800.0, None, 800.0, 800.0, 1900.0]
phones=[3.2, 71.2, 78.1, 259.5, 497.2, 7.8, 460.0, 549.9, 220.4, 195.7, 516.1, 565.5, 452.2, 137.1, 460.6, 281.3, 7.3, 481.9, 319.1, 462.6, 115.7, 9.7, 851.4, 14.3, 71.9, 215.4, 80.5, 225.3, 506.5, 237.2, 336.3, 7.0, 10.1, 3.4, 2.6, 5.7, 552.2, 169.6, 836.3, 2.3, 1.3, 213.0, 266.7, 176.2, 24.5, 0.2, 3.7, 289.9, 340.7, 14.6, 420.4, 74.7, None, 314.3, 614.6, 22.8, 304.8, 97.4, None, 125.6, 131.8, 142.4, 18.5, 7.9, 333.8, 8.2, 503.8, 112.6, 405.3, 586.4, 255.6, 194.5, 27.4, 26.8, 244.3, 146.6, 667.9, 14.4, 877.7, 589.7, 448.9, 364.5, 463.8, 492.0, 92.1, 842.4, 2.7, 7.4, 143.5, 16.9, 67.5, 546.7, 336.2, 647.7, 45.4, 52.0, 276.4, 38.6, 500.5, 676.0, 462.3, 430.9, 124.0, 461.2, 811.3, 104.5, 164.1, 8.1, 42.7, 42.4, 486.1, 211.0, 84.0, 14.1, 321.4, 255.6, 23.7, 2.3, 127.1, 585.5, 223.4, 515.4, 384.9, 260.0, 3.6, 7.9, 179.0, 90.0, 6.4, 505.0, 91.2, 394.4, 12.9, 289.3, 49.7, 181.6, 114.8, 208.1, 1035.6, 55.1, None, 40.4, 3.5, 62.6, 143.0, 15.9, 460.8, 365.3, 252.2, 441.7, 39.7, 1.9, 9.3, 254.7, 461.7, 85.5, 31.8, 325.6, 137.9, 10.9, 49.2, 79.5, 38.4, 306.3, 399.2, 283.1, 232.0, 380.9, 196.9, 280.6, 2.7, 293.3, 638.9, 303.3, 683.2, 190.9, 75.2, 704.3, 36.2, 140.6, 22.2, 285.8, 262.4, 4.0, 411.4, 220.1, 406.1, 13.4, 11.3, 107.0, 453.5, 61.5, 16.3, 184.7, 30.8, 715.0, 680.9, 153.8, 591.0, 33.5, 4.0, 108.9, 10.6, 97.7, 303.5, 123.6, 269.5, 74.6, 269.5, 59.3, 3.6, 259.9, 475.3, 543.5, 898.0, 291.4, 62.9, 32.6, 140.1, 187.7, 652.8, 118.6, 145.2, None, 37.2, 8.2, 26.8]
percent_literate = [36.0, 86.5, 70.0, 97.0, 100.0, 42.0, 95.0, 89.0, 97.1, 98.6, 97.0, 100.0, 98.0, 97.0, 95.6, 89.1, 43.1, 97.4, 99.6, 98.0, 94.1, 40.9, 98.0, 42.2, 87.2, None, 79.8, 86.4, 97.8, 93.9, 98.6, 26.6, 85.3, 51.6, 69.4, 79.0, 97.0, 76.6, 98.0, 51.0, 47.5, 96.2, 90.9, 92.5, 56.5, 5.5, 83.8, 95.0, 96.0, 50.9, 98.5, 97.0, 97.6, 99.9, 100.0, 67.9, 94.0, 84.7, 58.6, 92.5, 57.7, 80.2, 85.7, 58.6, 99.8, 42.7, None, 93.7, 100.0, 99.0, 83.0, 98.0, 63.2, 40.1, None, 99.0, 99.0, 74.8, None, 97.5, None, 98.0, 90.0, 99.0, 70.6, None, 35.9, 42.4, 98.8, 52.9, 6.2, 93.5, 99.4, 99.9, 59.5, 87.9, 79.4, 40.4, 98.0, None, 95.4, 98.6, 87.9, 99.0, None, 91.3, 98.4, 85.1, None, 99.0, 97.9, 83.5, 97.0, 66.4, 99.8, 87.4, 84.8, 57.5, 82.6, 100.0, 99.6, 100.0, 94.5, None, 68.9, 62.7, 88.7, 97.2, 46.4, 92.8, 93.7, 97.7, 41.7, 85.6, None, 2.2, 89.0, 99.1, 99.0, 97.8, 97.0, 51.7, 47.8, 84.0, None, 45.2, 99.0, 96.7, 91.0, 99.0, 67.5, 17.6, 68.0, 97.0, 100.0, 75.8, 45.7, 92.0, 92.6, 64.6, 94.0, 90.9, 92.6, 99.8, 93.3, 94.1, 82.5, 88.9, 98.4, 99.6, 70.4, 97.0, 97.0, 67.0, 99.0, 96.0, 99.7, 96.0, 79.3, 78.8, 40.2, 3.0, 58.0, 31.4, 92.5, None, 99.7, None, 37.8, 86.4, 97.9, 92.3, 61.1, 93.0, 81.6, 99.0, 99.0, 76.9, 96.1, 99.4, 78.2, 92.6, 60.9, 98.5, 98.6, 74.2, 86.5, 98.0, 98.0, None, 69.9, 99.7, 77.9, 99.0, 97.0, 98.0, 99.3, 53.0, 93.4, 90.3, None, 50.0, None, None, 50.2, 80.6, 0.7]<jupyter_output><empty_output><jupyter_text>- Create a scatter plot of GDP (`gdp`) vs. number of phones per 1000 people (`phones`).- Display the plot.<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>- Change the scatter plot so it displays the percent of the population that can read and write (`percent_literate`) on the y-axis.<jupyter_code># Change this scatter plot to have percent literate on the y-axis
sns.scatterplot(x=gdp, y=percent_literate)

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>*While this plot does not show a linear relationship between GDP and percent literate, countries with a lower GDP do seem more likely to have a lower percent of the population that can read and write.*  Making a count plot with a listIn 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`. - Use Seaborn to create a count plot with `region` on the y-axis.- Display the plot.<jupyter_code>region = countries_of_the_world['Region']
# Create count plot with region on the y-axis
sns.countplot(y=region)

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>*Sub-Saharan Africa contains the most countries in this list.*  Making a count plot with a DataFrameIn 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".<jupyter_code>young_people_survey = pd.read_csv('young-people-survey-responses.csv')<jupyter_output><empty_output><jupyter_text>- Use the `countplot()` function with the `x=` and `data=` arguments to create a count plot with the `"Spiders"` column values on the x-axis.- Display the plot.<jupyter_code># Create a count plot with "Spiders" on the x-axis
sns.countplot(x='Spiders', data=young_people_survey)

# Display the plot
plt.show()<jupyter_output><empty_output><jupyter_text>*This plot shows us that most young people reported not being afraid of spiders.*  Hue and scatter plotsLet'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).<jupyter_code>student_data = pd.read_csv('student_data.csv')<jupyter_output><empty_output><jupyter_text>- 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).<jupyter_code># Create a scatter plot of absences vs. final grade
sns.scatterplot(x='absences', y='G3', 
                data=student_data, hue='location')

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>- Make `"Rural"` appear before `"Urban"` in the plot legend.<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>*It looks like students with higher absences tend to have lower grades in both rural and urban areas.*  Hue and count plotsLet'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. - 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 the `student_data` DataFrame.    - Add subgroups to the plot using `"location"` variable and use the `palette_colors` dictionary to make the location subgroups green and blue.<jupyter_code># 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()<jupyter_output><empty_output><jupyter_text>*Students at GP tend to come from an urban location, but students at MS are more evenly split.*<jupyter_code>student_data.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1596471503934965, 'fraction_numerical': 0.22554700337282713, 'mean_word_length': 3.7161500815660684, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '591139', 'license': 'FSFAP', 'n_tokens_mistral': 6568, 'n_tokens_neox': 4711, 'n_words': 1594, 'path': 'Data Analyst with Python/06_Introduction_to_Data_Visualization_with_Seaborn/06_1_Introduction to Seaborn.ipynb', 'repo_name': 'brendensong/DataCamp', 'size': '11563'}