dataviz-solutions.ipynb 3.11 KB
Newer Older
George Mount's avatar
George Mount committed
1 2 3 4
{
 "cells": [
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
5
   "execution_count": null,
George Mount's avatar
George Mount committed
6
   "metadata": {},
George Mount's avatar
George Mount committed
7
   "outputs": [],
George Mount's avatar
George Mount committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   "source": [
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "\n",
    "housing = pd.read_csv('data/housing.csv')\n",
    "housing.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DRILLS\n",
    "\n",
    "For this exercise, analyze the `housing` dataset from the `data` folder: \n",
    "\n",
    "1. Plot a histogram of the `price` variable using 50 bins. \n",
    "2. Count the number of observations by number of `stories`.\n",
    "\n",
    "You can code this from scratch, or fill in the code below. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With a bar chart we will count up the observations of each value of a category. For example we could find how many observations are placed for each category. "
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
39
   "execution_count": null,
George Mount's avatar
George Mount committed
40
   "metadata": {},
George Mount's avatar
George Mount committed
41
   "outputs": [],
George Mount's avatar
George Mount committed
42 43 44 45 46 47 48
   "source": [
    "# Plot a histogram of price with 50 bins\n",
    "sns.distplot(housing['price'],bins=50)"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
49
   "execution_count": null,
George Mount's avatar
George Mount committed
50
   "metadata": {},
George Mount's avatar
George Mount committed
51
   "outputs": [],
George Mount's avatar
George Mount committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
   "source": [
    "# Count the number of observations by number of `stories`\n",
    "sns.countplot(housing['stories'], color='blue')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## DRILLS\n",
    "\n",
    "For this exercise, analyze the `housing` dataset from the `data` folder: \n",
    "\n",
    "1. Make a box plot of the distribution of `lotsize` for each value of the `prefarea` category.  \n",
    "2.  Make a scatterplot of `lotsize` versus `price`. Add a custom title to the plot.\n",
    "\n",
    "You can code this from scratch, or fill in the code below. "
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
73
   "execution_count": null,
George Mount's avatar
George Mount committed
74
   "metadata": {},
George Mount's avatar
George Mount committed
75
   "outputs": [],
George Mount's avatar
George Mount committed
76 77 78 79 80 81 82
   "source": [
    "# Box plot of the distribution of `lotsize` for each value of the `prefarea` category.  \n",
    "sns.boxplot(x='prefarea', y='lotsize', data=housing)"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
83
   "execution_count": null,
George Mount's avatar
George Mount committed
84
   "metadata": {},
George Mount's avatar
George Mount committed
85
   "outputs": [],
George Mount's avatar
George Mount committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
   "source": [
    "#  Make a scatterplot of `lotsize` versus `price`. Add a custom title to the plot.\n",
    "\n",
    "# First we need to call pyplot\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Now let's make our scatterplot\n",
    "sns.scatterplot(data=housing,x='lotsize',y='price')\n",
    "\n",
    "# Now let's add a custom title\n",
    "plt.title('Lot size versus sales price of houses')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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",
George Mount's avatar
George Mount committed
123
   "version": "3.7.9"
George Mount's avatar
George Mount committed
124 125 126 127 128
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}