{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Visualization, for Good and for Evil \n",
"\n",
"## Patrick Thornton\n",
"\n",
"\n",
"**Harvard University**
\n",
"**Winter 2021**
\n",
"\n",
"---"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas\n",
"import matplotlib.pyplot\n",
"import seaborn\n",
"from sklearn.datasets import load_iris"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load the iris dataset (you can ignore some of the nitty gritty here)\n",
"iris_obj = load_iris()\n",
"iris = pandas.DataFrame(iris_obj.data, columns=iris_obj.feature_names,index=pandas.Index([i for i in range(iris_obj.data.shape[0])])).join(pandas.DataFrame(iris_obj.target, columns=pandas.Index([\"species\"]), index=pandas.Index([i for i in range(iris_obj.target.shape[0])])))\n",
"iris.species.replace({0: 'setosa', 1: 'versicolor', 2: 'virginica'}, inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# jupyter notebooks will automatically print out this variable if you put it on its own line at the end of a cell\n",
"# this is a Pandas DataFrame\n",
"iris"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this is a Pandas Series\n",
"iris['species']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# only the rows with species = setosa\n",
"iris[iris['species'] == 'setosa']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# distributional plot: histogram\n",
"\n",
"# can adjust number and size of plots here; this means 1 plot (1 row, 1 column), size 10x10\n",
"fig, axis = pyplot.subplots(1, 1, figsize=(10, 10))\n",
"\n",
"# Seaborn's histogram function\n",
"seaborn.________(data=iris, x='petal length (cm)', ax=axis)\n",
"\n",
"# Setting various cosmetic plot parameters\n",
"axis.set_xlabel('petal length (cm)',fontsize=12)\n",
"axis.set_ylabel('frequency',fontsize=12)\n",
"axis.set_title('distribution of petal length in irises',fontsize=15)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# relational plot: scatterplot\n",
"\n",
"# can adjust number and size of plots here; this means 1 plot (1 row, 1 column), size 10x10\n",
"fig, axis = pyplot.subplots(1, 1, figsize=(10, 10))\n",
"\n",
"# Seaborn's scatterplot function\n",
"seaborn.________(data=iris, x='sepal length (cm)', y='sepal width (cm)', ax=axis)\n",
"\n",
"# Setting various cosmetic plot parameters\n",
"axis.set_xlabel('petal length (cm)',fontsize=12)\n",
"axis.set_ylabel('petal width (cm)',fontsize=12)\n",
"axis.set_title('petal length vs. petal width in irises',fontsize=15)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# categorical plot: barplot\n",
"\n",
"# can adjust number and size of plots here; this means 1 plot (1 row, 1 column), size 10x10\n",
"fig, axis = pyplot.subplots(1, 1, figsize=(10, 10))\n",
"\n",
"# Seaborn's barplot function - AUTOMATICALLY TAKES THE MEAN FOR EACH SPECIES\n",
"seaborn.________(data=iris, x='species', y='petal length (cm)', ax=axis)\n",
"\n",
"# Setting various cosmetic plot parameters\n",
"axis.set_xlabel('species of iris',fontsize=12)\n",
"axis.set_ylabel('average petal length (cm)',fontsize=12)\n",
"axis.set_title('species vs. average petal length in irises',fontsize=15)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}