data-visualization.ipynb 21.1 KB
Newer Older
George Mount's avatar
George Mount committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python for Data Analysis, pt. 2\n",
    "## Data visualization\n",
    "\n",
    "## Introducing `seaborn`\n",
    "\n",
    "There are several popular and equally advantageous data visualization packages in Python.  `seaborn` is one of them: let's take a look!\n",
    "\n",
    "### `seaborn` and `matplotlib`\n",
    "\n",
    "`seaborn` is built on `matplotlib`, similarly to how `pandas` is built on top of `numpy`. \n",
    "\n",
    "This package will help us build beautiful, statistically-informed visualizations that are optimized to work with `pandas` DataFrames.\n",
    "\n",
    "#### Loading `seaborn`\n",
    "\n",
    "If `seaborn` is not installed on your computer, you can do so with the below command:"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
27
   "execution_count": null,
George Mount's avatar
George Mount committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Install seaborn\n",
    "#!pip install seaborn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Aliasing `seaborn` \n",
    "\n",
    "If you remember one thing from this class, remember when and how to import modules! `sns` is a common alias for `seaborn`:"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
48
   "execution_count": 1,
George Mount's avatar
George Mount committed
49
   "metadata": {},
George Mount's avatar
George Mount committed
50
   "outputs": [],
George Mount's avatar
George Mount committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64
   "source": [
    "# Let's get started with seaborn!\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Great! Now that `seaborn` is ready to use, let's read in our data. We will be looking at vehicle mileage statistics. "
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mpg</th>\n",
       "      <th>cylinders</th>\n",
       "      <th>displacement</th>\n",
       "      <th>horsepower</th>\n",
       "      <th>weight</th>\n",
       "      <th>acceleration</th>\n",
       "      <th>model year</th>\n",
       "      <th>origin</th>\n",
       "      <th>car name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>8</td>\n",
       "      <td>400.0</td>\n",
       "      <td>230</td>\n",
       "      <td>4278</td>\n",
       "      <td>9.5</td>\n",
       "      <td>73</td>\n",
       "      <td>1</td>\n",
       "      <td>pontiac grand prix</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>14.0</td>\n",
       "      <td>8</td>\n",
       "      <td>455.0</td>\n",
       "      <td>225</td>\n",
       "      <td>4425</td>\n",
       "      <td>10.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>pontiac catalina</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>14.0</td>\n",
       "      <td>8</td>\n",
       "      <td>455.0</td>\n",
       "      <td>225</td>\n",
       "      <td>3086</td>\n",
       "      <td>10.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>buick estate wagon (sw)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>12.0</td>\n",
       "      <td>8</td>\n",
       "      <td>455.0</td>\n",
       "      <td>225</td>\n",
       "      <td>4951</td>\n",
       "      <td>11.0</td>\n",
       "      <td>73</td>\n",
       "      <td>1</td>\n",
       "      <td>buick electra 225 custom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>14.0</td>\n",
       "      <td>8</td>\n",
       "      <td>454.0</td>\n",
       "      <td>220</td>\n",
       "      <td>4354</td>\n",
       "      <td>9.0</td>\n",
       "      <td>70</td>\n",
       "      <td>1</td>\n",
       "      <td>chevrolet impala</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    mpg  cylinders  displacement  horsepower  weight  acceleration  \\\n",
       "0  16.0          8         400.0         230    4278           9.5   \n",
       "1  14.0          8         455.0         225    4425          10.0   \n",
       "2  14.0          8         455.0         225    3086          10.0   \n",
       "3  12.0          8         455.0         225    4951          11.0   \n",
       "4  14.0          8         454.0         220    4354           9.0   \n",
       "\n",
       "   model year  origin                  car name  \n",
       "0          73       1        pontiac grand prix  \n",
       "1          70       1          pontiac catalina  \n",
       "2          70       1   buick estate wagon (sw)  \n",
       "3          73       1  buick electra 225 custom  \n",
       "4          70       1          chevrolet impala  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
George Mount's avatar
George Mount committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
   "source": [
    "import pandas as pd\n",
    "mpg = pd.read_csv('data/mpg.csv')\n",
    "mpg.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's get started!\n",
    "\n",
    "Like with most topics in this class, we are just scratching the surface of what's possible with data visualization in Python. \n",
    "\n",
    "To learn more, check out the [`seaborn` documentation](https://seaborn.pydata.org) or the resources at the end of this course.\n",
    "\n",
    "You may be familiar with the types of charts we'll build here. They are all common practices and familiar to build in Excel.\n",
    "\n",
    "However, **we won't building graphs through a menu, but by coding it!** \n",
    "\n",
George Mount's avatar
George Mount committed
206 207 208 209 210 211 212 213 214 215 216 217
    "We'll get started plotting one variable, or *univariate* plotting, and then move to two, or *bivariate*. \n",
    "\n",
    "In `seaborn`, we'll use specific functions to create specific plots. We'll then specify what to plot using these arguments:\n",
    "\n",
    "| Argument | What it specifies                      |\n",
    "| -------- | -------------------------------------- |\n",
    "| `x`      | Which variable goes on the x-axis      |\n",
    "| `y`      | Which variable goes on the y-axis      |\n",
    "| `data`   | The data source (a DataFrame or array) |\n",
    "\n",
    "\n",
    "\n"
George Mount's avatar
George Mount committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Univariate plotting\n",
    "\n",
    "- Histograms\n",
    "- Count plots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Histograms\n",
    "\n",
    "A histogram displays how many observations are found within given intervals of a variable. \n",
    "\n",
    "We can use [`distplot()`](https://seaborn.pydata.org/generated/seaborn.distplot.html) to plot a histogram in `seaborn`.\n",
    "\n",
    "Let's look at the distribution of the `mpg` variable. \n",
    "\n",
    "We will pass a `pandas` Series into `distplot()` using bracket `[]` notation:"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "module 'seaborn' has no attribute 'displot'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-d258c79a8c1b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# Plot a histogram of mpg\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0msns\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdisplot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmpg\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmpg\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: module 'seaborn' has no attribute 'displot'"
     ]
    }
   ],
George Mount's avatar
George Mount committed
262 263
   "source": [
    "# Plot a histogram of mpg\n",
George Mount's avatar
George Mount committed
264
    "sns.distplot(x=mpg, data=mpg)"
George Mount's avatar
George Mount committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From this plot we can tell that cars range in mileage from about 10 to 45 mpg, with the most frequent mileages around 15-25 mpg.\n",
    "\n",
    "#### Customizing a histogram\n",
    "\n",
    "There are many options to [customize `displot()`](http://seaborn.pydata.org/generated/seaborn.distplot.html) -- let's customize the number of bins with the `bins` argument:"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
280
   "execution_count": null,
George Mount's avatar
George Mount committed
281 282 283
   "metadata": {
    "tags": []
   },
George Mount's avatar
George Mount committed
284
   "outputs": [],
George Mount's avatar
George Mount committed
285 286 287 288 289 290 291 292
   "source": [
    "# We can set our own number of bins \n",
    "# with bin = n\n",
    "sns.distplot(mpg['mpg'], bins=6)"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
293
   "execution_count": null,
George Mount's avatar
George Mount committed
294
   "metadata": {},
George Mount's avatar
George Mount committed
295
   "outputs": [],
George Mount's avatar
George Mount committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
   "source": [
    "# What happens if we increase the \n",
    "# number of bins? \n",
    "sns.distplot(mpg['mpg'], bins=30)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Count plots\n",
    "\n",
    "A count plot displays the number of observations per unique value of a category. \n",
    "\n",
    "We will use [`countplot()`](https://seaborn.pydata.org/generated/seaborn.countplot.html) to do this.\n",
    "\n",
    "For example, we can count the number of cars with 4 cylinders versus 6, etc. \n",
    "\n",
    "We'll again pass a Series into the `seaborn` method:"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
319
   "execution_count": null,
George Mount's avatar
George Mount committed
320
   "metadata": {},
George Mount's avatar
George Mount committed
321
   "outputs": [],
George Mount's avatar
George Mount committed
322 323 324 325 326 327 328 329
   "source": [
    "# Count the number of observations \n",
    "# per number of cylinders\n",
    "sns.countplot(mpg['cylinders'])"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
330
   "execution_count": null,
George Mount's avatar
George Mount committed
331
   "metadata": {},
George Mount's avatar
George Mount committed
332
   "outputs": [],
George Mount's avatar
George Mount committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
   "source": [
    "# If you are curious -- yes we can change the ordering\n",
    "# of the plot!\n",
    "# Count the number of observations \n",
    "# per number of cylinders\n",
    "sns.countplot(mpg['cylinders'], order=mpg['cylinders'].value_counts().index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Customizing `countplot()` colors\n",
    "\n",
    "In my opinion, filling each bar as different color isn't adding much information to the viewer and is therefore poor data visualization! \n",
    "\n",
    "(This is one place where Excel data visualizations do it better.)\n",
    "\n",
    "Fortunately, we can **set all bars to a consistent color** by fixing the `color` argument.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
357
   "execution_count": null,
George Mount's avatar
George Mount committed
358
   "metadata": {},
George Mount's avatar
George Mount committed
359
   "outputs": [],
George Mount's avatar
George Mount committed
360 361 362 363 364 365
   "source": [
    "sns.countplot(mpg['cylinders'], color='blue')"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
366
   "execution_count": null,
George Mount's avatar
George Mount committed
367
   "metadata": {},
George Mount's avatar
George Mount committed
368
   "outputs": [],
George Mount's avatar
George Mount committed
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
   "source": [
    "sns.countplot(mpg['cylinders'], order=mpg['cylinders'].value_counts().index, color='blue')"
   ]
  },
  {
   "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": "code",
George Mount's avatar
George Mount committed
389
   "execution_count": null,
George Mount's avatar
George Mount committed
390
   "metadata": {},
George Mount's avatar
George Mount committed
391
   "outputs": [],
George Mount's avatar
George Mount committed
392 393 394 395 396 397 398
   "source": [
    "# Plot a histogram of price with 50 bins\n",
    "sns.___(housing___,bins=___)"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
399
   "execution_count": null,
George Mount's avatar
George Mount committed
400
   "metadata": {},
George Mount's avatar
George Mount committed
401
   "outputs": [],
George Mount's avatar
George Mount committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
   "source": [
    "# Count the number of observations by number of `stories`\n",
    "___(___['___'], color='blue')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bivariate plotting\n",
    "\n",
    "Now let's look at ways to compare the values of two variable simultaneously. In particular, use the following charts:\n",
    "\n",
    "- Box plots\n",
    "- Bar plots\n",
    "- Scatter plots\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Box plots\n",
    "\n",
    "Box plots are depictions of the distribution of a variable by quartile. \n",
    "\n",
    "Our results will look like this:\n",
    "![Example box plot](images/box-plot.png)\n",
    "\n",
    "We will plot box plots with (big surprise!) [`boxplot()`](https://seaborn.pydata.org/generated/seaborn.boxplot.html).\n",
    "\n",
    "I would like to know the distribution of mileage by car origin.\n",
    "\n",
    "We are now plotting *two* variables, so our `seaborn` arguments will be a little different. We will include the following arguments:\n",
    "\n",
    "- `x`: the variable we want plotted on the x-axis\n",
    "- `y`: the variable we want plotted on the y-axis\n",
    "- `data`: The dataset where we can find these variables"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
445
   "execution_count": null,
George Mount's avatar
George Mount committed
446
   "metadata": {},
George Mount's avatar
George Mount committed
447
   "outputs": [],
George Mount's avatar
George Mount committed
448 449 450 451 452 453 454
   "source": [
    "# Create our box plot\n",
    "sns.boxplot(x='origin', y='horsepower', data=mpg, color='blue')"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
455
   "execution_count": null,
George Mount's avatar
George Mount committed
456
   "metadata": {},
George Mount's avatar
George Mount committed
457
   "outputs": [],
George Mount's avatar
George Mount committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
   "source": [
    "# Box plots can also be univariate\n",
    "sns.boxplot(mpg['horsepower'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Bar plots\n",
    "\n",
    "These are similar to count plots.\n",
    "\n",
    "We will continue to view values of a categorical variable along the x-axis, but this time our y-axis will be determined by values of another variable.\n",
    "\n",
    "For example, what if we wanted to know the average mileage for each number of cylinders? \n",
    "\n",
    "Let's find out using [`barplot()`](http://seaborn.pydata.org/generated/seaborn.barplot.html). \n",
    "\n",
    "By default, `barplot()` will sum the values on the Y-axis. We can change that with the `estimator` argument. \n",
    "\n",
    "We would like to know the average mileage of cars for each number of cylinders. "
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
484
   "execution_count": null,
George Mount's avatar
George Mount committed
485
   "metadata": {},
George Mount's avatar
George Mount committed
486
   "outputs": [],
George Mount's avatar
George Mount committed
487 488 489 490 491 492 493 494
   "source": [
    "# Let's import mean from numpy so we can\n",
    "# use it to find the average\n",
    "# A confidence interval is displayed by default.\n",
    "# 3- and 5-cylinder cars aren't too common! \n",
    "\n",
    "\n",
    "import numpy as np\n",
George Mount's avatar
George Mount committed
495
    "sns.barplot(x='cylinders', y='mpg', data=mpg, estimator=np.mean,\n",
George Mount's avatar
George Mount committed
496 497 498 499 500
    "            color='blue')\n"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
501
   "execution_count": null,
George Mount's avatar
George Mount committed
502
   "metadata": {},
George Mount's avatar
George Mount committed
503
   "outputs": [],
George Mount's avatar
George Mount committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
   "source": [
    "sns.countplot(mpg['cylinders'], order=mpg['cylinders'].value_counts().index, color='blue')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Scatterplots\n",
    "\n",
    "Scatterplots depict the relationship of two variables by placing each observation on an X-Y grid. \n",
    "\n",
    "We can do so in `seaborn` with [`scatterplot()`](https://seaborn.pydata.org/generated/seaborn.scatterplot.html). (Say what you will about `seaborn`, but the names are straightforward!)\n",
    "\n",
    "Let's find the relationship between `weight` and `mpg`."
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
523
   "execution_count": null,
George Mount's avatar
George Mount committed
524
   "metadata": {},
George Mount's avatar
George Mount committed
525
   "outputs": [],
George Mount's avatar
George Mount committed
526
   "source": [
George Mount's avatar
George Mount committed
527
    "sns.scatterplot(x='weight', y='mpg', data=mpg)"
George Mount's avatar
George Mount committed
528 529 530 531 532 533 534 535 536 537 538
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can color observations by category by using the `hue` argument."
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
539
   "execution_count": null,
George Mount's avatar
George Mount committed
540
   "metadata": {},
George Mount's avatar
George Mount committed
541
   "outputs": [],
George Mount's avatar
George Mount committed
542 543 544
   "source": [
    "# There are so few datapoints with 3 and 5 cylinders that it's no\n",
    "# included in our legend\n",
George Mount's avatar
George Mount committed
545
    "sns.scatterplot(x='weight', y='mpg', hue='cylinders', data=mpg)"
George Mount's avatar
George Mount committed
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Customizing our plots\n",
    "\n",
    "### With `seaborn`\n",
    "\n",
    "`seaborn` includes a number of options to customize the look of plots. [Read more about `seaborn` plot aesthetics at the documentation](https://seaborn.pydata.org/tutorial/aesthetics.html).\n",
    "\n",
    "We can do so with `set_style()`.\n",
    "\n",
    "For example, we can set the chart aesthetics to`darkgrid`:\n"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
565
   "execution_count": null,
George Mount's avatar
George Mount committed
566
   "metadata": {},
George Mount's avatar
George Mount committed
567
   "outputs": [],
George Mount's avatar
George Mount committed
568 569 570
   "source": [
    "# You will call the style before calling the plot\n",
    "sns.set_style('darkgrid')\n",
George Mount's avatar
George Mount committed
571
    "sns.scatterplot(x='weight', y='mpg' ,hue='cylinders', data=mpg)"
George Mount's avatar
George Mount committed
572 573 574 575 576 577 578 579 580 581 582
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By default, your chart aeshtetics specificed with `set_style()` will remain \"on\" for the remainder of your session. "
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
583
   "execution_count": null,
George Mount's avatar
George Mount committed
584
   "metadata": {},
George Mount's avatar
George Mount committed
585
   "outputs": [],
George Mount's avatar
George Mount committed
586
   "source": [
George Mount's avatar
George Mount committed
587
    "sns.scatterplot(x='weight', y='mpg', data=mpg)"
George Mount's avatar
George Mount committed
588 589 590 591 592 593 594 595 596 597 598
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can reset these aesthetics with `reset_orig()`."
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
599
   "execution_count": null,
George Mount's avatar
George Mount committed
600 601 602 603 604 605 606 607 608
   "metadata": {},
   "outputs": [],
   "source": [
    "# Clear back to the chart aesthetics\n",
    "sns.reset_orig()"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
609
   "execution_count": null,
George Mount's avatar
George Mount committed
610
   "metadata": {},
George Mount's avatar
George Mount committed
611
   "outputs": [],
George Mount's avatar
George Mount committed
612
   "source": [
George Mount's avatar
George Mount committed
613
    "sns.scatterplot(x='weight', y='mpg', data=mpg)"
George Mount's avatar
George Mount committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### With `matplotlib` \n",
    "\n",
    "It's good 'data hygeine' to include legible axes and labels to our plot.\n",
    "\n",
    "`seaborn` relies on `matplotlib` to do this. \n",
    "\n",
    "We will need to import the [`pyplot` module from `matplotlib` to do this](https://matplotlib.org/api/pyplot_api.html).\n",
    "\n",
    "It's common to alias this as `plt`.\n"
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
633
   "execution_count": null,
George Mount's avatar
George Mount committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
   "metadata": {},
   "outputs": [],
   "source": [
    "# Call in matplotlib.pyplot\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's do a few things:\n",
    "\n",
    "1. Add a custom x-axis label with `xlabel()`.\n",
    "2. Add a custom y-axis label with `ylabel()`.\n",
    "3. Add a custom plot title with `title()`."
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
654
   "execution_count": null,
George Mount's avatar
George Mount committed
655
   "metadata": {},
George Mount's avatar
George Mount committed
656
   "outputs": [],
George Mount's avatar
George Mount committed
657
   "source": [
George Mount's avatar
George Mount committed
658
    "sns.scatterplot(x='weight', y='mpg', data=mpg)\n",
George Mount's avatar
George Mount committed
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    "plt.xlabel('Weight (pounds)')\n",
    "plt.ylabel('Mileage (miles per gallon)')\n",
    "plt.title('Vehicle weight versus mileage')"
   ]
  },
  {
   "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
680
   "execution_count": null,
George Mount's avatar
George Mount committed
681
   "metadata": {},
George Mount's avatar
George Mount committed
682
   "outputs": [],
George Mount's avatar
George Mount committed
683 684
   "source": [
    "# Box plot of the distribution of `lotsize` for each value of the `prefarea` category.  \n",
George Mount's avatar
George Mount committed
685
    "___.boxplot(___=___, x=___, ___=housing)"
George Mount's avatar
George Mount committed
686 687 688 689
   ]
  },
  {
   "cell_type": "code",
George Mount's avatar
George Mount committed
690
   "execution_count": null,
George Mount's avatar
George Mount committed
691
   "metadata": {},
George Mount's avatar
George Mount committed
692
   "outputs": [],
George Mount's avatar
George Mount committed
693 694 695 696 697 698 699
   "source": [
    "#  Make a scatterplot of `lotsize` versus `price`. Add a custom title to the plot.\n",
    "\n",
    "# First we need to call pyplot\n",
    "___ ___ ___ plt\n",
    "\n",
    "# Now let's make our scatterplot\n",
George Mount's avatar
George Mount committed
700
    "sns.___(___=housing, x='lotsize', y='price')\n",
George Mount's avatar
George Mount committed
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
    "\n",
    "# Now let's add a custom title\n",
    "plt.___('Lot size versus sales price of houses')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Questions about data visualization in Python?"
   ]
  },
  {
   "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
737
   "version": "3.7.4"
George Mount's avatar
George Mount committed
738 739 740 741 742
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}