{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "PREVIOUS_MAX_ROWS = pd.options.display.max_rows\n",
    "pd.options.display.max_rows = 20\n",
    "pd.options.display.max_colwidth = 80\n",
    "pd.options.display.max_columns = 20\n",
    "np.random.seed(12345)\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib\n",
    "plt.rc(\"figure\", figsize=(10, 6))\n",
    "np.set_printoptions(precision=4, suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.arange(10)\n",
    "data\n",
    "plt.plot(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax1 = fig.add_subplot(2, 2, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax2 = fig.add_subplot(2, 2, 2)\n",
    "ax3 = fig.add_subplot(2, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax3.plot(np.random.standard_normal(50).cumsum(), color=\"black\",\n",
    "         linestyle=\"dashed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax1.hist(np.random.standard_normal(100), bins=20, color=\"black\", alpha=0.3);\n",
    "ax2.scatter(np.arange(30), np.arange(30) + 3 * np.random.standard_normal(30));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 3)\n",
    "axes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)\n",
    "for i in range(2):\n",
    "    for j in range(2):\n",
    "        axes[i, j].hist(np.random.standard_normal(500), bins=50,\n",
    "                        color=\"black\", alpha=0.5)\n",
    "fig.subplots_adjust(wspace=0, hspace=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = fig.add_subplot()\n",
    "ax.plot(np.random.standard_normal(30).cumsum(), color=\"black\",\n",
    "        linestyle=\"dashed\", marker=\"o\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure()\n",
    "ax = fig.add_subplot()\n",
    "data = np.random.standard_normal(30).cumsum()\n",
    "ax.plot(data, color=\"black\", linestyle=\"dashed\", label=\"Default\");\n",
    "ax.plot(data, color=\"black\", linestyle=\"dashed\",\n",
    "        drawstyle=\"steps-post\", label=\"steps-post\");\n",
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(np.random.standard_normal(1000).cumsum());"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "ticks = ax.set_xticks([0, 250, 500, 750, 1000])\n",
    "labels = ax.set_xticklabels([\"one\", \"two\", \"three\", \"four\", \"five\"],\n",
    "                            rotation=30, fontsize=8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax.set_xlabel(\"Stages\")\n",
    "ax.set_title(\"My first matplotlib plot\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\n",
    "ax.plot(np.random.randn(1000).cumsum(), color=\"black\", label=\"one\");\n",
    "ax.plot(np.random.randn(1000).cumsum(), color=\"black\", linestyle=\"dashed\",\n",
    "        label=\"two\");\n",
    "ax.plot(np.random.randn(1000).cumsum(), color=\"black\", linestyle=\"dotted\",\n",
    "        label=\"three\");"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "\n",
    "data = pd.read_csv(\"examples/spx.csv\", index_col=0, parse_dates=True)\n",
    "spx = data[\"SPX\"]\n",
    "\n",
    "spx.plot(ax=ax, color=\"black\")\n",
    "\n",
    "crisis_data = [\n",
    "    (datetime(2007, 10, 11), \"Peak of bull market\"),\n",
    "    (datetime(2008, 3, 12), \"Bear Stearns Fails\"),\n",
    "    (datetime(2008, 9, 15), \"Lehman Bankruptcy\")\n",
    "]\n",
    "\n",
    "for date, label in crisis_data:\n",
    "    ax.annotate(label, xy=(date, spx.asof(date) + 75),\n",
    "                xytext=(date, spx.asof(date) + 225),\n",
    "                arrowprops=dict(facecolor=\"black\", headwidth=4, width=2,\n",
    "                                headlength=4),\n",
    "                horizontalalignment=\"left\", verticalalignment=\"top\")\n",
    "\n",
    "# Zoom in on 2007-2010\n",
    "ax.set_xlim([\"1/1/2007\", \"1/1/2011\"])\n",
    "ax.set_ylim([600, 1800])\n",
    "\n",
    "ax.set_title(\"Important dates in the 2008\u20132009 financial crisis\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax.set_title(\"Important dates in the 2008\u20132009 financial crisis\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(12, 6))\n",
    "rect = plt.Rectangle((0.2, 0.75), 0.4, 0.15, color=\"black\", alpha=0.3)\n",
    "circ = plt.Circle((0.7, 0.2), 0.15, color=\"blue\", alpha=0.3)\n",
    "pgon = plt.Polygon([[0.15, 0.15], [0.35, 0.4], [0.2, 0.6]],\n",
    "                   color=\"green\", alpha=0.5)\n",
    "ax.add_patch(rect)\n",
    "ax.add_patch(circ)\n",
    "ax.add_patch(pgon)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = pd.Series(np.random.standard_normal(10).cumsum(), index=np.arange(0, 100, 10))\n",
    "s.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.standard_normal((10, 4)).cumsum(0),\n",
    "                  columns=[\"A\", \"B\", \"C\", \"D\"],\n",
    "                  index=np.arange(0, 100, 10))\n",
    "plt.style.use('grayscale')\n",
    "df.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2, 1)\n",
    "data = pd.Series(np.random.uniform(size=16), index=list(\"abcdefghijklmnop\"))\n",
    "data.plot.bar(ax=axes[0], color=\"black\", alpha=0.7)\n",
    "data.plot.barh(ax=axes[1], color=\"black\", alpha=0.7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(12348)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(np.random.uniform(size=(6, 4)),\n",
    "                  index=[\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"],\n",
    "                  columns=pd.Index([\"A\", \"B\", \"C\", \"D\"], name=\"Genus\"))\n",
    "df\n",
    "df.plot.bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.plot.barh(stacked=True, alpha=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "tips = pd.read_csv(\"examples/tips.csv\")\n",
    "tips.head()\n",
    "party_counts = pd.crosstab(tips[\"day\"], tips[\"size\"])\n",
    "party_counts = party_counts.reindex(index=[\"Thur\", \"Fri\", \"Sat\", \"Sun\"])\n",
    "party_counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "party_counts = party_counts.loc[:, 2:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Normalize to sum to 1\n",
    "party_pcts = party_counts.div(party_counts.sum(axis=\"columns\"),\n",
    "                              axis=\"index\")\n",
    "party_pcts\n",
    "party_pcts.plot.bar(stacked=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "\n",
    "tips[\"tip_pct\"] = tips[\"tip\"] / (tips[\"total_bill\"] - tips[\"tip\"])\n",
    "tips.head()\n",
    "sns.barplot(x=\"tip_pct\", y=\"day\", data=tips, orient=\"h\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(x=\"tip_pct\", y=\"day\", hue=\"time\", data=tips, orient=\"h\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.close(\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"whitegrid\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "tips[\"tip_pct\"].plot.hist(bins=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "tips[\"tip_pct\"].plot.density()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "comp1 = np.random.standard_normal(200)\n",
    "comp2 = 10 + 2 * np.random.standard_normal(200)\n",
    "values = pd.Series(np.concatenate([comp1, comp2]))\n",
    "\n",
    "sns.histplot(values, bins=100, color=\"black\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "macro = pd.read_csv(\"examples/macrodata.csv\")\n",
    "data = macro[[\"cpi\", \"m1\", \"tbilrate\", \"unemp\"]]\n",
    "trans_data = np.log(data).diff().dropna()\n",
    "trans_data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = sns.regplot(x=\"m1\", y=\"unemp\", data=trans_data)\n",
    "ax.set_title(\"Changes in log(m1) versus log(unemp)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pairplot(trans_data, diag_kind=\"kde\", plot_kws={\"alpha\": 0.2})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.catplot(x=\"day\", y=\"tip_pct\", hue=\"time\", col=\"smoker\",\n",
    "            kind=\"bar\", data=tips[tips.tip_pct < 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.catplot(x=\"day\", y=\"tip_pct\", row=\"time\",\n",
    "            col=\"smoker\",\n",
    "            kind=\"bar\", data=tips[tips.tip_pct < 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.catplot(x=\"tip_pct\", y=\"day\", kind=\"box\",\n",
    "            data=tips[tips.tip_pct < 0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.options.display.max_rows = PREVIOUS_MAX_ROWS"
   ]
  }
 ],
 "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
}