{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Seaborn: A Statistical Data Visualization Package\n", "## Styles" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:20:55.079993Z", "start_time": "2018-10-18T19:20:54.941780Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import scipy as sp\n", "import matplotlib.pyplot as plt\n", "\n", "def sinplot(flip=1):\n", " x = np.linspace(0, 14, 100)\n", " for i in range(1, 7):\n", " plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)\n", " \n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:21:22.163942Z", "start_time": "2018-10-18T19:21:21.411336Z" } }, "outputs": [], "source": [ "import seaborn as sb\n", "sb.set()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:21:25.887993Z", "start_time": "2018-10-18T19:21:25.741890Z" } }, "outputs": [], "source": [ "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:21:56.374360Z", "start_time": "2018-10-18T19:21:56.236539Z" } }, "outputs": [], "source": [ "sb.set_style(\"whitegrid\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:22:00.656105Z", "start_time": "2018-10-18T19:22:00.526089Z" } }, "outputs": [], "source": [ "sb.set_style(\"dark\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:22:02.109100Z", "start_time": "2018-10-18T19:22:01.987408Z" } }, "outputs": [], "source": [ "sb.set_style(\"white\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:22:03.784652Z", "start_time": "2018-10-18T19:22:03.647763Z" } }, "outputs": [], "source": [ "sb.set_style(\"ticks\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:22:54.395798Z", "start_time": "2018-10-18T19:22:54.256695Z" } }, "outputs": [], "source": [ "sinplot()\n", "sb.despine(offset=10, trim=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:23:05.758350Z", "start_time": "2018-10-18T19:23:05.620395Z" } }, "outputs": [], "source": [ "sb.set_style(\"whitegrid\")\n", "sinplot()\n", "sb.despine(left=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:23:45.131149Z", "start_time": "2018-10-18T19:23:44.912515Z" } }, "outputs": [], "source": [ "with sb.axes_style(\"darkgrid\"):\n", " plt.subplot(211)\n", " sinplot()\n", "plt.subplot(212)\n", "sinplot(-1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:23:58.426456Z", "start_time": "2018-10-18T19:23:58.415804Z" }, "scrolled": true }, "outputs": [], "source": [ "sb.axes_style(\"dark\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:24:43.206841Z", "start_time": "2018-10-18T19:24:43.080326Z" } }, "outputs": [], "source": [ "sb.set_style(\"dark\", {\"axes.facecolor\": \".1\"})\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:24:51.548451Z", "start_time": "2018-10-18T19:24:51.410072Z" } }, "outputs": [], "source": [ "sb.set()\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:25:10.509559Z", "start_time": "2018-10-18T19:25:10.371515Z" } }, "outputs": [], "source": [ "sb.set_context(\"paper\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:25:18.280237Z", "start_time": "2018-10-18T19:25:18.139966Z" } }, "outputs": [], "source": [ "sb.set_context(\"notebook\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:25:25.692292Z", "start_time": "2018-10-18T19:25:25.549366Z" } }, "outputs": [], "source": [ "sb.set_context(\"talk\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:25:31.990278Z", "start_time": "2018-10-18T19:25:31.861755Z" } }, "outputs": [], "source": [ "sb.set_context(\"poster\")\n", "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:25:59.001551Z", "start_time": "2018-10-18T19:25:58.847377Z" } }, "outputs": [], "source": [ "sb.set(context='paper', rc={\"figure.figsize\": (10, 6)})\n", "sinplot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Color Palettes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:07.151845Z", "start_time": "2018-10-18T19:27:07.089231Z" } }, "outputs": [], "source": [ "current_palette = sb.color_palette(\"deep\")\n", "sb.palplot(current_palette)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:16.428477Z", "start_time": "2018-10-18T19:27:16.369602Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"pastel\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:22.102369Z", "start_time": "2018-10-18T19:27:22.043010Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"bright\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:27.474381Z", "start_time": "2018-10-18T19:27:27.373552Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"dark\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:32.804108Z", "start_time": "2018-10-18T19:27:32.745823Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"colorblind\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:27:39.955449Z", "start_time": "2018-10-18T19:27:39.891031Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"Blues\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:28:19.157854Z", "start_time": "2018-10-18T19:28:19.099240Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"BuGn_r\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:28:24.863075Z", "start_time": "2018-10-18T19:28:24.801046Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"Paired\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:28:45.503272Z", "start_time": "2018-10-18T19:28:45.433158Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"Set2\", n_colors=10, desat=.8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:29:14.406464Z", "start_time": "2018-10-18T19:29:14.341154Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"husl\", n_colors=8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:29:19.221990Z", "start_time": "2018-10-18T19:29:19.153309Z" } }, "outputs": [], "source": [ "sb.palplot(sb.color_palette(\"hls\", 8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:29:40.043670Z", "start_time": "2018-10-18T19:29:39.975524Z" }, "scrolled": true }, "outputs": [], "source": [ "sb.palplot(sb.hls_palette(8, l=.3, s=.8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:30:02.934744Z", "start_time": "2018-10-18T19:30:02.856860Z" } }, "outputs": [], "source": [ "sb.palplot(sb.husl_palette(8, l=.3, s=.8))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:30:10.924153Z", "start_time": "2018-10-18T19:30:10.860461Z" } }, "outputs": [], "source": [ "sb.palplot(sb.light_palette(\"purple\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:30:15.300876Z", "start_time": "2018-10-18T19:30:15.237197Z" } }, "outputs": [], "source": [ "sb.palplot(sb.dark_palette(\"purple\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:30:31.791427Z", "start_time": "2018-10-18T19:30:31.722547Z" } }, "outputs": [], "source": [ "sb.palplot(sb.blend_palette([\"firebrick\", \"palegreen\"], 8)) " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-17T17:27:07.365061Z", "start_time": "2018-10-17T17:27:07.301281Z" } }, "outputs": [], "source": [ "sb.palplot(sb.diverging_palette(220, 20, n=7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:31:08.524421Z", "start_time": "2018-10-18T19:31:08.441482Z" } }, "outputs": [], "source": [ "sb.diverging_palette?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:32:52.819454Z", "start_time": "2018-10-18T19:32:52.750403Z" } }, "outputs": [], "source": [ "sb.palplot(sb.diverging_palette(255, 133, l=60, n=7, center=\"dark\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:33:17.986983Z", "start_time": "2018-10-18T19:33:17.924787Z" } }, "outputs": [], "source": [ "flatui = [\"#9b59b6\", \"#3498db\", \"#95a5a6\", \"#e74c3c\", \"#34495e\", \"#2ecc71\"]\n", "sb.palplot(sb.color_palette(flatui))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:33:27.044789Z", "start_time": "2018-10-18T19:33:26.941278Z" } }, "outputs": [], "source": [ "colors = [\"windows blue\", \"amber\", \"greyish\", \"faded green\", \"dusty purple\", \"wisteria\"]\n", "sb.palplot(sb.xkcd_palette(colors))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:33:35.989877Z", "start_time": "2018-10-18T19:33:35.959909Z" } }, "outputs": [], "source": [ "sb.xkcd_rgb" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:33:57.665957Z", "start_time": "2018-10-18T19:33:57.596566Z" } }, "outputs": [], "source": [ "sb.palplot(sb.cubehelix_palette(8, start=1.5, rot=1.75, dark=0.5, light=.95, reverse=True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:34:05.124157Z", "start_time": "2018-10-18T19:34:04.960000Z" } }, "outputs": [], "source": [ "with sb.xkcd_palette(colors):\n", " sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:34:28.659269Z", "start_time": "2018-10-18T19:34:28.507180Z" } }, "outputs": [], "source": [ "sinplot()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:34:39.170029Z", "start_time": "2018-10-18T19:34:39.013535Z" } }, "outputs": [], "source": [ "sb.set_palette(sb.color_palette(\"Dark2\", 8))\n", "sinplot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Density Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:35:31.517328Z", "start_time": "2018-10-18T19:35:31.329599Z" } }, "outputs": [], "source": [ "x = np.random.normal(size=200)\n", "sb.distplot(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:36:05.371157Z", "start_time": "2018-10-18T19:36:05.191946Z" } }, "outputs": [], "source": [ "sb.distplot(x, bins=20)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:36:17.691225Z", "start_time": "2018-10-18T19:36:17.328504Z" } }, "outputs": [], "source": [ "sb.distplot(x, bins=20, kde=False, rug=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:38:55.013778Z", "start_time": "2018-10-18T19:38:54.353415Z" } }, "outputs": [], "source": [ "f, axes = plt.subplots(2, 2, figsize=(10, 10), sharex=True)\n", "sb.despine(left=True)\n", "\n", "sb.distplot(x, kde=False, color=\"b\", ax=axes[0, 0])\n", "\n", "sb.distplot(x, hist=False, rug=True, color=\"r\", ax=axes[0, 1])\n", "\n", "sb.distplot(x, hist=False, color=\"g\", kde_kws={\"shade\": True}, ax=axes[1, 0])\n", "\n", "sb.distplot(x, color=\"y\", ax=axes[1, 1])\n", "\n", "#plt.setp(axes, yticks=[])\n", "#plt.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:41:17.679272Z", "start_time": "2018-10-18T19:41:17.489723Z" } }, "outputs": [], "source": [ "x = np.random.normal(size=15)\n", "support = np.linspace(-4, 4, 100)\n", "bandwidth = 0.7\n", "for x_i in x:\n", " plt.plot(support, sp.stats.norm(x_i, bandwidth).pdf(support))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:41:23.536621Z", "start_time": "2018-10-18T19:41:23.237580Z" } }, "outputs": [], "source": [ "for x_i in x:\n", " plt.plot(support, sp.stats.norm(x_i, bandwidth).pdf(support))\n", "sb.rugplot(x, color=\".2\", linewidth=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:42:04.087033Z", "start_time": "2018-10-18T19:42:04.062986Z" } }, "outputs": [], "source": [ "kernels = []\n", "for x_i in x:\n", " kernel = sp.stats.norm(x_i, bandwidth).pdf(support)\n", " kernels.append(kernel)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:42:18.170088Z", "start_time": "2018-10-18T19:42:17.975043Z" } }, "outputs": [], "source": [ "density = np.sum(kernels, axis=0)\n", "plt.plot(support, density)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:42:29.957904Z", "start_time": "2018-10-18T19:42:29.809832Z" } }, "outputs": [], "source": [ "density /= sp.integrate.trapz(density, support)\n", "plt.plot(support, density)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:43:09.132102Z", "start_time": "2018-10-18T19:43:08.994366Z" } }, "outputs": [], "source": [ "sb.kdeplot(x, shade=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:43:47.223448Z", "start_time": "2018-10-18T19:43:47.011663Z" } }, "outputs": [], "source": [ "sb.kdeplot(x, bw='scott', label=\"bw: scott\")\n", "sb.kdeplot(x, bw='silverman', label=\"bw: silverman\")\n", "sb.kdeplot(x, bw=.2, label=\"bw: 0.2\")\n", "sb.kdeplot(x, bw=2, label=\"bw: 2\")\n", "sb.rugplot(x)\n", "plt.ylim(0, 0.8)\n", "plt.legend()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:44:47.794009Z", "start_time": "2018-10-18T19:44:47.602929Z" } }, "outputs": [], "source": [ "x = np.random.normal(size=30)\n", "sb.kdeplot(x, shade=True, cut=0)\n", "sb.rugplot(x)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:46:46.302439Z", "start_time": "2018-10-18T19:46:45.850337Z" } }, "outputs": [], "source": [ "x = np.random.gamma(6, size=200)\n", "\n", "plt.subplot(221)\n", "sb.distplot(x, kde=False, hist=True, fit=sp.stats.norm)\n", "\n", "plt.subplot(222)\n", "sb.distplot(x, kde=False, hist=True, fit=sp.stats.laplace)\n", "\n", "plt.subplot(223)\n", "sb.distplot(x, kde=False, hist=True, fit=sp.stats.expon)\n", "\n", "plt.subplot(224)\n", "sb.distplot(x, kde=False, hist=True, fit=sp.stats.gamma)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bivariate Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:49:31.670014Z", "start_time": "2018-10-18T19:49:31.663717Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "sb.set()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:50:43.773775Z", "start_time": "2018-10-18T19:50:43.769834Z" }, "scrolled": true }, "outputs": [], "source": [ "mean, cov = [0, 1], [(1, .95), (.95, 1)]\n", "data = np.random.multivariate_normal(mean, cov, 200)\n", "x, y = data.T[0], data.T[1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:51:03.985570Z", "start_time": "2018-10-18T19:51:03.797041Z" } }, "outputs": [], "source": [ "sb.distplot(x)\n", "sb.distplot(y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:53:15.535635Z", "start_time": "2018-10-18T19:53:15.243601Z" } }, "outputs": [], "source": [ "sb.jointplot(x, y, size=7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:53:32.846304Z", "start_time": "2018-10-18T19:53:32.545207Z" } }, "outputs": [], "source": [ "sb.jointplot(x, y, size=7, kind='hex')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:57:06.476890Z", "start_time": "2018-10-18T19:57:05.591890Z" } }, "outputs": [], "source": [ "sb.jointplot(x, y, size=7, kind='kde')\n", "plt.savefig('kde2d.pdf')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:54:16.713675Z", "start_time": "2018-10-18T19:54:15.451783Z" } }, "outputs": [], "source": [ "f, ax = plt.subplots(figsize=(7, 7))\n", "sb.kdeplot(x, y, ax=ax)\n", "sb.rugplot(x, color=\"g\", ax=ax)\n", "sb.rugplot(y, vertical=True, ax=ax)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:57:46.074375Z", "start_time": "2018-10-18T19:57:45.244458Z" } }, "outputs": [], "source": [ "f, ax = plt.subplots(figsize=(7, 7))\n", "cmap = sb.cubehelix_palette(as_cmap=True, dark=0, light=1, reverse=True)\n", "sb.kdeplot(x, y, ax=ax, cmap=cmap, n_levels=60, shade=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T19:58:56.667898Z", "start_time": "2018-10-18T19:58:55.610432Z" } }, "outputs": [], "source": [ "g = sb.jointplot(x, y, kind=\"kde\")\n", "g.plot_joint(plt.scatter, c=\"w\", s=30, linewidth=1, marker=\"+\")\n", "g.ax_joint.collections[0].set_alpha(0)\n", "g.set_axis_labels(\"$X$\", \"$Y$\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:00:57.515500Z", "start_time": "2018-10-18T20:00:53.214132Z" } }, "outputs": [], "source": [ "rs = np.random.RandomState(50)\n", "\n", "# Set up the matplotlib figure\n", "f, axes = plt.subplots(3, 3, figsize=(9, 9), sharex=True, sharey=True)\n", "\n", "# Rotate the starting point around the cubehelix hue circle\n", "for ax, s in zip(axes.flat, np.linspace(0, 3, 10)):\n", " # Create a cubehelix colormap to use with kdeplot\n", " cmap = sb.cubehelix_palette(start=s, light=1, as_cmap=True)\n", " # Generate and plot a random bivariate dataset\n", " x, y = rs.randn(2, 50)\n", " sb.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax)\n", " ax.set(xlim=(-3, 3), ylim=(-3, 3))\n", "\n", "f.tight_layout()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with Dataframes" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:02:37.775861Z", "start_time": "2018-10-18T20:02:37.632562Z" }, "scrolled": true }, "outputs": [], "source": [ "tips = sb.load_dataset(\"tips\")\n", "tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:04:04.810267Z", "start_time": "2018-10-18T20:04:04.633319Z" } }, "outputs": [], "source": [ "sb.countplot(x=tips['day'], hue=tips['sex'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:06:00.164929Z", "start_time": "2018-10-18T20:06:00.003224Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.countplot(x=\"day\", hue=\"sex\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:06:16.574269Z", "start_time": "2018-10-18T20:06:16.419110Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.countplot(y=\"day\", hue=\"sex\", data=tips, palette='hls')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:07:10.090484Z", "start_time": "2018-10-18T20:07:09.825924Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.barplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:08:11.904176Z", "start_time": "2018-10-18T20:08:11.633141Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.pointplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:08:55.843456Z", "start_time": "2018-10-18T20:08:55.507633Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.pointplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, \n", " palette={\"Male\": \"#943684\", \"Female\": \"#528257\"}, \n", " markers=[\"^\", \"o\"], linestyles=[\"-\", \"--\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:09:26.621086Z", "start_time": "2018-10-18T20:09:26.491463Z" } }, "outputs": [], "source": [ "sb.stripplot(x=\"day\", y=\"total_bill\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:10:29.659784Z", "start_time": "2018-10-18T20:10:29.521732Z" } }, "outputs": [], "source": [ "sb.stripplot(x=\"day\", y=\"total_bill\", data=tips, jitter=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:11:01.512608Z", "start_time": "2018-10-18T20:11:01.314229Z" } }, "outputs": [], "source": [ "sb.swarmplot(x=\"time\", y=\"tip\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:13:08.794448Z", "start_time": "2018-10-18T20:13:08.619149Z" } }, "outputs": [], "source": [ "sb.swarmplot(x=\"size\", y=\"total_bill\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:13:56.303121Z", "start_time": "2018-10-18T20:13:56.060777Z" } }, "outputs": [], "source": [ "sb.swarmplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:14:37.048587Z", "start_time": "2018-10-18T20:14:36.872841Z" } }, "outputs": [], "source": [ "sb.swarmplot(x=\"total_bill\", y=\"day\", hue=\"time\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:15:42.696490Z", "start_time": "2018-10-18T20:15:42.496502Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.boxplot(x=\"day\", y=\"total_bill\", hue=\"time\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:18:43.919409Z", "start_time": "2018-10-18T20:18:43.849883Z" }, "scrolled": true }, "outputs": [], "source": [ "tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:19:18.568966Z", "start_time": "2018-10-18T20:19:18.518245Z" }, "scrolled": true }, "outputs": [], "source": [ "tips[\"weekend\"] = tips[\"day\"].isin([\"Sat\", \"Sun\"])\n", "tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:19:48.804128Z", "start_time": "2018-10-18T20:19:48.617018Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.boxplot(x=\"day\", y=\"total_bill\", hue=\"weekend\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:21:34.033569Z", "start_time": "2018-10-18T20:21:33.840875Z" } }, "outputs": [], "source": [ "sb.boxplot(x=\"day\", y=\"total_bill\", data=tips)\n", "sb.swarmplot(x=\"day\", y=\"total_bill\", data=tips, size=5, color=\".3\", linewidth=0)\n", "sb.despine(trim=True, left=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:22:13.731676Z", "start_time": "2018-10-18T20:22:13.494341Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"total_bill\", y=\"day\", hue=\"sex\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:23:07.582130Z", "start_time": "2018-10-18T20:23:07.344743Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, bw=.2, scale=\"count\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:23:52.711733Z", "start_time": "2018-10-18T20:23:52.504687Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, bw=.1, scale=\"count\", \n", " split=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:24:11.547392Z", "start_time": "2018-10-18T20:24:10.952034Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, split=True, inner=\"stick\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:24:28.615399Z", "start_time": "2018-10-18T20:24:28.393322Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, split=True, inner=\"quart\")\n", "sb.despine(left=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:24:44.492649Z", "start_time": "2018-10-18T20:24:44.301049Z" } }, "outputs": [], "source": [ "sb.violinplot(x=\"day\", y=\"total_bill\", data=tips, inner=None)\n", "sb.swarmplot(x=\"day\", y=\"total_bill\", data=tips, color=\"w\", alpha=.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:25:17.019136Z", "start_time": "2018-10-18T20:25:16.703415Z" } }, "outputs": [], "source": [ "sb.factorplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, size=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:25:45.446542Z", "start_time": "2018-10-18T20:25:45.070647Z" } }, "outputs": [], "source": [ "sb.factorplot(x=\"day\", y=\"total_bill\", hue=\"sex\", data=tips, size=6, kind=\"bar\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:26:40.313488Z", "start_time": "2018-10-18T20:26:40.270605Z" }, "scrolled": true }, "outputs": [], "source": [ "tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:26:47.864673Z", "start_time": "2018-10-18T20:26:47.600050Z" } }, "outputs": [], "source": [ "sb.factorplot(x=\"day\", hue=\"size\", data=tips, size=6, kind=\"count\", palette=\"BuPu\", aspect=1.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:28:03.557643Z", "start_time": "2018-10-18T20:28:03.226599Z" } }, "outputs": [], "source": [ "sb.factorplot(x=\"day\", y=\"total_bill\", hue=\"smoker\", \n", " col=\"time\", data=tips, size=6, kind=\"swarm\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:29:09.978737Z", "start_time": "2018-10-18T20:29:09.433770Z" } }, "outputs": [], "source": [ "sb.factorplot(x=\"time\", y=\"total_bill\", hue=\"smoker\", col=\"day\", data=tips, \n", " kind=\"box\", size=6, aspect=.5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:29:36.541128Z", "start_time": "2018-10-18T20:29:36.495187Z" }, "scrolled": true }, "outputs": [], "source": [ "tips" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:30:51.022516Z", "start_time": "2018-10-18T20:30:49.240932Z" } }, "outputs": [], "source": [ "g = sb.PairGrid(tips, x_vars=[\"smoker\", \"time\", \"sex\"], \n", " y_vars=[\"total_bill\", \"tip\", \"size\"], aspect=.75, size=6)\n", "g.map(sb.swarmplot, palette=\"pastel\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bivariates Plots" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:31:29.388187Z", "start_time": "2018-10-18T20:31:29.332129Z" }, "scrolled": true }, "outputs": [], "source": [ "iris = sb.load_dataset('iris')\n", "iris" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:32:02.893017Z", "start_time": "2018-10-18T20:32:02.588976Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.jointplot(x=\"sepal_length\", y=\"sepal_width\", data=iris, size=7)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:32:15.507268Z", "start_time": "2018-10-18T20:32:14.163771Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.pairplot(iris)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:33:24.750443Z", "start_time": "2018-10-18T20:33:22.997959Z" }, "scrolled": false }, "outputs": [], "source": [ "sb.pairplot(iris, hue=\"species\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:35:15.114673Z", "start_time": "2018-10-18T20:35:10.843666Z" }, "scrolled": false }, "outputs": [], "source": [ "g = sb.PairGrid(iris[iris.species == 'versicolor'])\n", "g.map_diag(sb.distplot, norm_hist=True, bins='auto', kde_kws={'cumulative': True})\n", "g.map_lower(sb.kdeplot, cmap=\"Blues_d\", n_levels=6)\n", "g.map_upper(plt.scatter)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:37:44.703614Z", "start_time": "2018-10-18T20:37:43.582826Z" } }, "outputs": [], "source": [ "# Subset the iris dataset by species\n", "setosa = iris.query(\"species == 'setosa'\")\n", "virginica = iris.query(\"species == 'virginica'\")\n", "\n", "# Set up the figure\n", "f, ax = plt.subplots(figsize=(8, 8))\n", "ax.set_aspect(\"equal\")\n", "\n", "# Draw the two density plots\n", "ax = sb.kdeplot(setosa.sepal_width, setosa.sepal_length,\n", " cmap=\"Reds\", shade=True, shade_lowest=False)\n", "ax = sb.kdeplot(virginica.sepal_width, virginica.sepal_length,\n", " cmap=\"Blues\", shade=True, shade_lowest=False)\n", "\n", "# Add labels to the plot\n", "red = sb.color_palette(\"Reds\")[-2]\n", "blue = sb.color_palette(\"Blues\")[-2]\n", "ax.text(2.5, 8.2, \"virginica\", size=16, color=blue)\n", "ax.text(3.8, 4.5, \"setosa\", size=16, color=red)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear Regression" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:38:20.031884Z", "start_time": "2018-10-18T20:38:19.983110Z" }, "scrolled": true }, "outputs": [], "source": [ "anscombe = sb.load_dataset(\"anscombe\")\n", "anscombe" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:38:41.154449Z", "start_time": "2018-10-18T20:38:40.908988Z" } }, "outputs": [], "source": [ "sb.regplot(x=\"x\", y=\"y\", data=anscombe.query(\"dataset == 'I'\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:39:07.136643Z", "start_time": "2018-10-18T20:39:06.835727Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"petal_length\", y=\"sepal_length\", data=iris, size=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:39:42.508999Z", "start_time": "2018-10-18T20:39:41.576245Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"petal_length\", y=\"sepal_length\", data=iris, size=6, x_estimator=np.mean)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:39:52.995569Z", "start_time": "2018-10-18T20:39:52.744921Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"x\", y=\"y\", data=anscombe.query(\"dataset == 'II'\"), size=6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:40:04.686429Z", "start_time": "2018-10-18T20:40:04.366015Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"x\", y=\"y\", data=anscombe.query(\"dataset == 'II'\"), size=6, order=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:40:24.612319Z", "start_time": "2018-10-18T20:40:24.225749Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"petal_length\", y=\"sepal_length\", data=iris, size=6, order=2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:40:34.788358Z", "start_time": "2018-10-18T20:40:34.388069Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"x\", y=\"y\", data=anscombe.query(\"dataset == 'III'\"), size=6, \n", " x_estimator=np.mean)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:41:06.865747Z", "start_time": "2018-10-18T20:40:59.442947Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"x\", y=\"y\", data=anscombe.query(\"dataset == 'III'\"), size=6, robust=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:42:05.678019Z", "start_time": "2018-10-18T20:42:05.258109Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"x\", y=\"y\", col=\"dataset\", hue=\"dataset\", data=anscombe, \n", " col_wrap=2, ci=None, palette=\"muted\", size=4, scatter_kws={\"s\": 50, \"alpha\": 1})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:42:16.563303Z", "start_time": "2018-10-18T20:42:16.041130Z" } }, "outputs": [], "source": [ "# Plot tip as a function of toal bill across days\n", "g = sb.lmplot(x=\"sepal_length\", y=\"sepal_width\", hue=\"species\", \n", " truncate=True, size=8, data=iris)\n", "g.set_axis_labels(\"Sepal length (mm)\", \"Sepal width (mm)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:43:59.586894Z", "start_time": "2018-10-18T20:43:59.320835Z" } }, "outputs": [], "source": [ "tips[\"big_tip\"] = (tips.tip / tips.total_bill) > .15\n", "sb.lmplot(x=\"total_bill\", y=\"big_tip\", data=tips, y_jitter=.03)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:44:23.143336Z", "start_time": "2018-10-18T20:44:20.815141Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"total_bill\", y=\"big_tip\", data=tips, y_jitter=.03, logistic=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:44:38.217680Z", "start_time": "2018-10-18T20:44:33.978850Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"total_bill\", y=\"big_tip\", data=tips, y_jitter=.03, logistic=True, hue=\"time\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:45:02.581962Z", "start_time": "2018-10-18T20:45:02.334218Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"total_bill\", y=\"tip\", data=tips, y_jitter=.03, lowess=True, hue=\"smoker\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:45:39.781501Z", "start_time": "2018-10-18T20:45:39.003481Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"total_bill\", y=\"tip\", hue=\"smoker\", col=\"time\", data=tips)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:45:55.194866Z", "start_time": "2018-10-18T20:45:53.889185Z" } }, "outputs": [], "source": [ "sb.lmplot(x=\"total_bill\", y=\"tip\", hue=\"smoker\", col=\"time\", row=\"sex\", data=tips)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:46:39.680363Z", "start_time": "2018-10-18T20:46:39.457012Z" } }, "outputs": [], "source": [ "# Initialize the figure\n", "f, ax = plt.subplots(figsize=(15, 5))\n", "sb.despine(bottom=True, left=True)\n", "\n", "iris2 = pd.melt(iris, \"species\", var_name=\"measurement\")\n", " \n", "# Show each observation with a scatterplot\n", "sb.stripplot(x=\"value\", y=\"measurement\", hue=\"species\",\n", " data=iris2, jitter=True, alpha=.5)\n", "\n", "# Show the conditional means\n", "sb.pointplot(x=\"value\", y=\"measurement\", hue=\"species\", data=iris2,\n", " dodge=.5, join=False, palette=\"dark\", markers=\"d\", scale=.9, ci=None)\n", "\n", "# Improve the legend\n", "handles, labels = ax.get_legend_handles_labels()\n", "ax.legend(handles[3:], labels[3:], title=\"species\", handletextpad=0, columnspacing=1,\n", " loc=\"lower right\", ncol=3, frameon=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:46:46.895440Z", "start_time": "2018-10-18T20:46:46.837256Z" }, "scrolled": true }, "outputs": [], "source": [ "flights_long = sb.load_dataset(\"flights\")\n", "flights_long" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:46:51.005099Z", "start_time": "2018-10-18T20:46:50.975513Z" } }, "outputs": [], "source": [ "flights = flights_long.pivot(\"month\", \"year\", \"passengers\")\n", "flights" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:46:56.535563Z", "start_time": "2018-10-18T20:46:56.032271Z" } }, "outputs": [], "source": [ "f, ax = plt.subplots(figsize=(9, 6))\n", "sb.heatmap(flights, annot=True, fmt=\"d\", linewidths=.5, ax=ax)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:47:11.410391Z", "start_time": "2018-10-18T20:47:11.058900Z" } }, "outputs": [], "source": [ "from string import ascii_letters\n", "\n", "sb.set(style=\"white\")\n", "\n", "# Generate a large random dataset\n", "rs = np.random.RandomState(33)\n", "d = pd.DataFrame(data=rs.normal(size=(100, 26)), columns=list(ascii_letters[26:]))\n", "\n", "# Compute the correlation matrix\n", "corr = d.corr()\n", "\n", "# Generate a mask for the upper triangle\n", "mask = np.zeros_like(corr, dtype=np.bool)\n", "mask[np.triu_indices_from(mask)] = True\n", "\n", "# Set up the matplotlib figure\n", "f, ax = plt.subplots(figsize=(11, 9))\n", "\n", "# Generate a custom diverging colormap\n", "cmap = sb.diverging_palette(220, 10, as_cmap=True)\n", "\n", "# Draw the heatmap with the mask and correct aspect ratio\n", "sb.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0, square=True, linewidths=.5, cbar_kws={\"shrink\": .5})" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:47:28.303452Z", "start_time": "2018-10-18T20:47:28.131101Z" }, "scrolled": true }, "outputs": [], "source": [ "brain_networks = sb.load_dataset(\"brain_networks\", header=[0, 1, 2], index_col=0)\n", "used_networks = [1, 5, 6, 7, 8, 12, 13, 17]\n", "used_columns = (brain_networks.columns.get_level_values(\"network\").astype(int).isin(used_networks))\n", "brain_networks = brain_networks.loc[:, used_columns]\n", "brain_networks" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:47:31.801268Z", "start_time": "2018-10-18T20:47:31.041327Z" } }, "outputs": [], "source": [ "network_pal = sb.husl_palette(8, s=.45)\n", "network_lut = dict(zip(map(str, used_networks), network_pal))\n", "networks = brain_networks.columns.get_level_values(\"network\")\n", "network_colors = pd.Series(networks, index=brain_networks.columns).map(network_lut)\n", "\n", "sb.clustermap(brain_networks.corr(), center=0, cmap=\"BrBG\", \n", " row_colors=network_colors, col_colors=network_colors, linewidths=.75, figsize=(13, 13))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:48:11.007136Z", "start_time": "2018-10-18T20:48:10.785133Z" } }, "outputs": [], "source": [ "# Compute the correlation matrix and average over networks\n", "corr_df = brain_networks.corr().groupby(level=\"network\").mean()\n", "corr_df.index = corr_df.index.astype(int)\n", "corr_df = corr_df.sort_index().T\n", "\n", "# Set up the matplotlib figure\n", "f, ax = plt.subplots(figsize=(11, 6))\n", "\n", "# Draw a violinplot with a narrower bandwidth than the default\n", "sb.violinplot(data=corr_df, palette=\"Set3\", bw=.2, cut=1, linewidth=1)\n", "\n", "# Finalize the figure\n", "ax.set(ylim=(-.7, 1.05))\n", "sb.despine(left=True, bottom=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:48:14.582130Z", "start_time": "2018-10-18T20:48:14.093798Z" } }, "outputs": [], "source": [ "# Generate an example radial datast\n", "r = np.linspace(0, 10, num=300)\n", "df = pd.DataFrame({'r': r, 'slow': r, 'medium': 2 * r, 'fast': 4 * r})\n", "\n", "# Convert the dataframe to long-form or \"tidy\" format\n", "df = pd.melt(df, id_vars=['r'], var_name='speed', value_name='theta')\n", "\n", "# Set up a grid of axes with a polar projection\n", "g = sb.FacetGrid(df, col=\"speed\", hue=\"speed\", subplot_kws=dict(projection='polar'), size=4.5, sharex=False, sharey=False, despine=False)\n", "\n", "# Draw a scatterplot onto each axes in the grid\n", "g.map(plt.scatter, \"theta\", \"r\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:48:32.972074Z", "start_time": "2018-10-18T20:48:32.888916Z" }, "scrolled": true }, "outputs": [], "source": [ "gammas = sb.load_dataset(\"gammas\")\n", "gammas" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:48:33.930736Z", "start_time": "2018-10-18T20:48:33.281035Z" } }, "outputs": [], "source": [ "sb.tsplot(data=gammas, time=\"timepoint\", unit=\"subject\", condition=\"ROI\", value=\"BOLD signal\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2018-10-18T20:48:37.774629Z", "start_time": "2018-10-18T20:48:36.684905Z" } }, "outputs": [], "source": [ "sines = []\n", "rs = np.random.RandomState(8)\n", "for _ in range(15):\n", " x = np.linspace(0, 30 / 2, 30)\n", " y = np.sin(x) + rs.normal(0, 1.5) + rs.normal(0, .3, 30)\n", " sines.append(y)\n", "\n", "# Plot the average over replicates with bootstrap resamples\n", "sb.tsplot(sines, err_style=\"boot_traces\", n_boot=500)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.6.5" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }