Lecture 7 – Data Visualization 📈

DSC 10, Fall 2022

Announcements

Agenda

Don't forget about the DSC 10 Reference Sheet and the Resources tab of the course website!

Aside: keyboard shortcuts

There are several keyboard shortcuts built into Jupyter Notebooks designed to help you save time. To see them, either click the keyboard button in the toolbar above or hit the H key on your keyboard (as long as you're not actively editing a cell).

Particularly useful shortcuts:

Action Keyboard shortcut
Run cell + jump to next cell SHIFT + ENTER
Save the notebook CTRL/CMD + S
Create new cell above/below A/B
Delete cell DD
Convert cell to Markdown M
Convert cell to code Y

Note: the latter three only work if you're not actively editing a cell (to exit "edit mode", click somewhere outside of a cell).

Why visualize?

Run these cells to load the Little Women data from Lecture 1.

Little Women

In Lecture 1, we were able to answer questions about the plot of Little Women without having to read the novel. Some of those questions included:

Napoleon's March

John Snow

Why visualize?

Terminology

Individuals and variables

Types of variables

There are two main types of variables:

Examples of numerical variables

Examples of categorical variables

Concept Check ✅ – Answer at cc.dsc10.com

Which of these is not a numerical variable?

A. Fuel economy in miles per gallon.

B. Number of quarters at UCSD.

C. College at UCSD (Sixth, Seventh, etc).

D. Bank account number.

E. More than one of these are not numerical variables.

Types of visualizations

The type of visualization we create depends on the kinds of variables we're visualizing.

Note: We may interchange the words "plot", "chart", and "graph"; they all mean the same thing.

Scatter plots

Dataset of 50 top-grossing actors

Column Contents

'Actor'|Name of actor 'Total Gross'| Total gross domestic box office receipt, in millions of dollars, of all of the actor’s movies 'Number of Movies'| The number of movies the actor has been in 'Average per Movie'| Total gross divided by number of movies '#1 Movie'| The highest grossing movie the actor has been in 'Gross'| Gross domestic box office receipt, in millions of dollars, of the actor’s #1 Movie

Scatter plots

What is the relationship between 'Number of Movies' and 'Total Gross'?

Scatter plots

Scatter plots

What is the relationship between 'Number of Movies' and 'Average per Movie'?

Note that in the above plot, there's a negative association and an outlier.

Who was in 60 or more movies?

Who is the outlier?

Whoever they are, they made very few, high grossing movies.

Anthony Daniels

Line plots 📉

Dataset aggregating movies by year

Column Content

'Year'| Year 'Total Gross in Billions'| Total domestic box office gross, in billions of dollars, of all movies released 'Number of Movies'| Number of movies released '#1 Movie'| Highest grossing movie

Line plots

How has the number of movies changed over time? 🤔

Line plots

Plotting tip

Since the year 2000

We can create a line plot of just 2000 onwards by querying movies_by_year before calling .plot.

What do you think explains the declines around 2008 and 2020?

How did this affect total gross?

What was the top grossing movie of 2016? 🐟

Bar charts 📊

Dataset of the global top 200 songs on Spotify as of Tuesday (10/4/22)

Downloaded from here – check it out!

Bar charts

How many streams do the top 10 songs have?

Bar charts

How many songs do the top 15 artists have in the top 200?

First, let's create a DataFrame with a single column that describes the number of songs in the top 200 per artist. This involves using .groupby with .count(). Since we want one row per artist, we will group by 'artist_names'.

Using .sort_values and .take, we'll keep just the top 15 artists. Note that all columns in songs_per_artist contain the same information (this is a consequence of using .count()).

Using .assign and .drop, we'll create a column named 'count' that contains the same information that the other 3 columns contain, and then .get only that column (or equivalently, drop the other 3 columns).

Before calling .plot(kind='barh', y='count'), we'll sort top_15_artists by 'count' in increasing order. This is because, weirdly, Python reverses the order of rows when creating bars in horizontal bar charts.

Vertical bar charts

To create a vertical bar chart, use kind='bar' instead of kind='barh'. These are typically harder to read, though.

Aside: How many streams did Justin Bieber's songs on the chart receive?

It seems like we're missing a popular song...

Answer: Using .str.contains.

Fun demo 🎵

Let's find the URI of a song we care about.

Watch what happens! 🎶

Try it out yourself!

Summary

Summary

Let's discuss!