{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exploring a DataSet\n",
"\n",
"Now that we have seen the basic pieces of Altair's API, it's time to practice using it to explore a new dataset.\n",
"With your partner, choose one of the following four datasets, detailed below.\n",
"\n",
"As you explore the data, recall the building blocks we've discussed:\n",
"\n",
"- various marks: ``mark_point()``, ``mark_line()``, ``mark_tick()``, ``mark_bar()``, ``mark_area()``, ``mark_rect()``, etc.\n",
"- various encodings: ``x``, ``y``, ``color``, ``shape``, ``size``, ``row``, ``column``, ``text``, ``tooltip``, etc.\n",
"- binning and aggregations: a [List of available aggregations](https://altair-viz.github.io/user_guide/encoding.html#binning-and-aggregation) can be found in Altair's documentation\n",
"- stacking and layering (``alt.layer`` <-> ``+``, ``alt.hconcat`` <-> ``|``, ``alt.vconcat`` <-> ``&``)\n",
"\n",
"Start simple and build from there. Which encodings work best with quantitative data? With categorical data?\n",
"What can you learn about your dataset using these tools?\n",
"\n",
"We'll set aside about 20 minutes for you to work on this with your partner."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from vega_datasets import data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Seattle Weather\n",
"\n",
"This data includes daily precipitation, temperature range, wind speed, and weather type as a function of date between 2012 and 2015 in Seattle."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" date | \n",
" precipitation | \n",
" temp_max | \n",
" temp_min | \n",
" wind | \n",
" weather | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2012-01-01 | \n",
" 0.0 | \n",
" 12.8 | \n",
" 5.0 | \n",
" 4.7 | \n",
" drizzle | \n",
"
\n",
" \n",
" 1 | \n",
" 2012-01-02 | \n",
" 10.9 | \n",
" 10.6 | \n",
" 2.8 | \n",
" 4.5 | \n",
" rain | \n",
"
\n",
" \n",
" 2 | \n",
" 2012-01-03 | \n",
" 0.8 | \n",
" 11.7 | \n",
" 7.2 | \n",
" 2.3 | \n",
" rain | \n",
"
\n",
" \n",
" 3 | \n",
" 2012-01-04 | \n",
" 20.3 | \n",
" 12.2 | \n",
" 5.6 | \n",
" 4.7 | \n",
" rain | \n",
"
\n",
" \n",
" 4 | \n",
" 2012-01-05 | \n",
" 1.3 | \n",
" 8.9 | \n",
" 2.8 | \n",
" 6.1 | \n",
" rain | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" date precipitation temp_max temp_min wind weather\n",
"0 2012-01-01 0.0 12.8 5.0 4.7 drizzle\n",
"1 2012-01-02 10.9 10.6 2.8 4.5 rain\n",
"2 2012-01-03 0.8 11.7 7.2 2.3 rain\n",
"3 2012-01-04 20.3 12.2 5.6 4.7 rain\n",
"4 2012-01-05 1.3 8.9 2.8 6.1 rain"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"weather = data.seattle_weather()\n",
"weather.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Gapminder\n",
"\n",
"This data consists of population, fertility, and life expectancy over time in a number of countries around the world.\n",
"\n",
"Note that, while you may be tempted to use a temporal encoding for the year, here the year is simply a number, not a date stamp, and so temporal encoding is not the best choice here."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" year | \n",
" country | \n",
" cluster | \n",
" pop | \n",
" life_expect | \n",
" fertility | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1955 | \n",
" Afghanistan | \n",
" 0 | \n",
" 8891209 | \n",
" 30.332 | \n",
" 7.7 | \n",
"
\n",
" \n",
" 1 | \n",
" 1960 | \n",
" Afghanistan | \n",
" 0 | \n",
" 9829450 | \n",
" 31.997 | \n",
" 7.7 | \n",
"
\n",
" \n",
" 2 | \n",
" 1965 | \n",
" Afghanistan | \n",
" 0 | \n",
" 10997885 | \n",
" 34.020 | \n",
" 7.7 | \n",
"
\n",
" \n",
" 3 | \n",
" 1970 | \n",
" Afghanistan | \n",
" 0 | \n",
" 12430623 | \n",
" 36.088 | \n",
" 7.7 | \n",
"
\n",
" \n",
" 4 | \n",
" 1975 | \n",
" Afghanistan | \n",
" 0 | \n",
" 14132019 | \n",
" 38.438 | \n",
" 7.7 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" year country cluster pop life_expect fertility\n",
"0 1955 Afghanistan 0 8891209 30.332 7.7\n",
"1 1960 Afghanistan 0 9829450 31.997 7.7\n",
"2 1965 Afghanistan 0 10997885 34.020 7.7\n",
"3 1970 Afghanistan 0 12430623 36.088 7.7\n",
"4 1975 Afghanistan 0 14132019 38.438 7.7"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gapminder = data.gapminder()\n",
"gapminder.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Population\n",
"\n",
"This data contains the US population sub-divided by age and sex every decade from 1850 to near the present.\n",
"\n",
"Note that, while you may be tempted to use a temporal encoding for the year, here the year is simply a number, not a date stamp, and so temporal encoding is not the best choice."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" year | \n",
" age | \n",
" sex | \n",
" people | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1850 | \n",
" 0 | \n",
" 1 | \n",
" 1483789 | \n",
"
\n",
" \n",
" 1 | \n",
" 1850 | \n",
" 0 | \n",
" 2 | \n",
" 1450376 | \n",
"
\n",
" \n",
" 2 | \n",
" 1850 | \n",
" 5 | \n",
" 1 | \n",
" 1411067 | \n",
"
\n",
" \n",
" 3 | \n",
" 1850 | \n",
" 5 | \n",
" 2 | \n",
" 1359668 | \n",
"
\n",
" \n",
" 4 | \n",
" 1850 | \n",
" 10 | \n",
" 1 | \n",
" 1260099 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" year age sex people\n",
"0 1850 0 1 1483789\n",
"1 1850 0 2 1450376\n",
"2 1850 5 1 1411067\n",
"3 1850 5 2 1359668\n",
"4 1850 10 1 1260099"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"population = data.population()\n",
"population.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Movies\n",
"\n",
"The movies dataset has data on 3200 movies, including release date, budget, and ratings on IMDB and Rotten Tomatoes."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Title | \n",
" US_Gross | \n",
" Worldwide_Gross | \n",
" US_DVD_Sales | \n",
" Production_Budget | \n",
" Release_Date | \n",
" MPAA_Rating | \n",
" Running_Time_min | \n",
" Distributor | \n",
" Source | \n",
" Major_Genre | \n",
" Creative_Type | \n",
" Director | \n",
" Rotten_Tomatoes_Rating | \n",
" IMDB_Rating | \n",
" IMDB_Votes | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" The Land Girls | \n",
" 146083.0 | \n",
" 146083.0 | \n",
" NaN | \n",
" 8000000.0 | \n",
" Jun 12 1998 | \n",
" R | \n",
" NaN | \n",
" Gramercy | \n",
" None | \n",
" None | \n",
" None | \n",
" None | \n",
" NaN | \n",
" 6.1 | \n",
" 1071.0 | \n",
"
\n",
" \n",
" 1 | \n",
" First Love, Last Rites | \n",
" 10876.0 | \n",
" 10876.0 | \n",
" NaN | \n",
" 300000.0 | \n",
" Aug 07 1998 | \n",
" R | \n",
" NaN | \n",
" Strand | \n",
" None | \n",
" Drama | \n",
" None | \n",
" None | \n",
" NaN | \n",
" 6.9 | \n",
" 207.0 | \n",
"
\n",
" \n",
" 2 | \n",
" I Married a Strange Person | \n",
" 203134.0 | \n",
" 203134.0 | \n",
" NaN | \n",
" 250000.0 | \n",
" Aug 28 1998 | \n",
" None | \n",
" NaN | \n",
" Lionsgate | \n",
" None | \n",
" Comedy | \n",
" None | \n",
" None | \n",
" NaN | \n",
" 6.8 | \n",
" 865.0 | \n",
"
\n",
" \n",
" 3 | \n",
" Let's Talk About Sex | \n",
" 373615.0 | \n",
" 373615.0 | \n",
" NaN | \n",
" 300000.0 | \n",
" Sep 11 1998 | \n",
" None | \n",
" NaN | \n",
" Fine Line | \n",
" None | \n",
" Comedy | \n",
" None | \n",
" None | \n",
" 13.0 | \n",
" NaN | \n",
" NaN | \n",
"
\n",
" \n",
" 4 | \n",
" Slam | \n",
" 1009819.0 | \n",
" 1087521.0 | \n",
" NaN | \n",
" 1000000.0 | \n",
" Oct 09 1998 | \n",
" R | \n",
" NaN | \n",
" Trimark | \n",
" Original Screenplay | \n",
" Drama | \n",
" Contemporary Fiction | \n",
" None | \n",
" 62.0 | \n",
" 3.4 | \n",
" 165.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Title US_Gross Worldwide_Gross US_DVD_Sales \\\n",
"0 The Land Girls 146083.0 146083.0 NaN \n",
"1 First Love, Last Rites 10876.0 10876.0 NaN \n",
"2 I Married a Strange Person 203134.0 203134.0 NaN \n",
"3 Let's Talk About Sex 373615.0 373615.0 NaN \n",
"4 Slam 1009819.0 1087521.0 NaN \n",
"\n",
" Production_Budget Release_Date MPAA_Rating Running_Time_min Distributor \\\n",
"0 8000000.0 Jun 12 1998 R NaN Gramercy \n",
"1 300000.0 Aug 07 1998 R NaN Strand \n",
"2 250000.0 Aug 28 1998 None NaN Lionsgate \n",
"3 300000.0 Sep 11 1998 None NaN Fine Line \n",
"4 1000000.0 Oct 09 1998 R NaN Trimark \n",
"\n",
" Source Major_Genre Creative_Type Director \\\n",
"0 None None None None \n",
"1 None Drama None None \n",
"2 None Comedy None None \n",
"3 None Comedy None None \n",
"4 Original Screenplay Drama Contemporary Fiction None \n",
"\n",
" Rotten_Tomatoes_Rating IMDB_Rating IMDB_Votes \n",
"0 NaN 6.1 1071.0 \n",
"1 NaN 6.9 207.0 \n",
"2 NaN 6.8 865.0 \n",
"3 13.0 NaN NaN \n",
"4 62.0 3.4 165.0 "
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"movies = data.movies()\n",
"movies.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}