{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ".. _aesthetics_tutorial:\n",
    "\n",
    ".. currentmodule:: seaborn"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Controlling figure aesthetics\n",
    "=============================\n"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Drawing attractive figures is important. When making figures for yourself, as you explore a dataset, it's nice to have plots that are pleasant to look at. Visualizations are also central to communicating quantitative insights to an audience, and in that setting it's even more necessary to have figures that catch the attention and draw a viewer in.\n",
    "\n",
    "Matplotlib is highly customizable, but it can be hard to know what settings to tweak to achieve an attractive plot. Seaborn comes with a number of customized themes and a high-level interface for controlling the look of matplotlib figures."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "np.random.seed(sum(map(ord, \"aesthetics\")))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Let's define a simple function to plot some offset sine waves, which will help us see the different stylistic parameters we can tweak."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sinplot(n=10, flip=1):\n",
    "    x = np.linspace(0, 14, 100)\n",
    "    for i in range(1, n + 1):\n",
    "        plt.plot(x, np.sin(x + i * .5) * (n + 2 - i) * flip)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "This is what the plot looks like with matplotlib defaults:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "To switch to seaborn defaults, simply call the :func:`set_theme` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_theme()\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "(Note that in versions of seaborn prior to 0.8, :func:`set_theme` was called on import. On later versions, it must be explicitly invoked).\n",
    "\n",
    "Seaborn splits matplotlib parameters into two independent groups. The first group sets the aesthetic style of the plot, and the second scales various elements of the figure so that it can be easily incorporated into different contexts.\n",
    "\n",
    "The interface for manipulating these parameters are two pairs of functions. To control the style, use the :func:`axes_style` and :func:`set_style` functions. To scale the plot, use the :func:`plotting_context` and :func:`set_context` functions. In both cases, the first function returns a dictionary of parameters and the second sets the matplotlib defaults.\n",
    "\n",
    ".. _axes_style:\n",
    "\n",
    "Seaborn figure styles\n",
    "---------------------\n",
    "\n",
    "There are five preset seaborn themes: ``darkgrid``, ``whitegrid``, ``dark``, ``white``, and ``ticks``. They are each suited to different applications and personal preferences. The default theme is ``darkgrid``. As mentioned above, the grid helps the plot serve as a lookup table for quantitative information, and the white-on grey helps to keep the grid from competing with lines that represent data. The ``whitegrid`` theme is similar, but it is better suited to plots with heavy data elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"whitegrid\")\n",
    "data = np.random.normal(size=(20, 6)) + np.arange(6) / 2\n",
    "sns.boxplot(data=data);"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "For many plots, (especially for settings like talks, where you primarily want to use figures to provide impressions of patterns in the data), the grid is less necessary."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"dark\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"white\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Sometimes you might want to give a little extra structure to the plots, which is where ticks come in handy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"ticks\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ".. _remove_spines:\n",
    "\n",
    "Removing axes spines\n",
    "--------------------\n",
    "\n",
    "Both the ``white`` and ``ticks`` styles can benefit from removing the top and right axes spines, which are not needed. The seaborn function :func:`despine` can be called to remove them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sinplot()\n",
    "sns.despine()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Some plots benefit from offsetting the spines away from the data, which can also be done when calling :func:`despine`. When the ticks don't cover the whole range of the axis, the ``trim`` parameter will limit the range of the surviving spines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f, ax = plt.subplots()\n",
    "sns.violinplot(data=data)\n",
    "sns.despine(offset=10, trim=True);"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You can also control which spines are removed with additional arguments to :func:`despine`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"whitegrid\")\n",
    "sns.boxplot(data=data, palette=\"deep\")\n",
    "sns.despine(left=True)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Temporarily setting figure style\n",
    "--------------------------------\n",
    "\n",
    "Although it's easy to switch back and forth, you can also use the :func:`axes_style` function in a ``with`` statement to temporarily set plot parameters. This also allows you to make figures with differently-styled axes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = plt.figure(figsize=(6, 6))\n",
    "gs = f.add_gridspec(2, 2)\n",
    "\n",
    "with sns.axes_style(\"darkgrid\"):\n",
    "    ax = f.add_subplot(gs[0, 0])\n",
    "    sinplot(6)\n",
    "    \n",
    "with sns.axes_style(\"white\"):\n",
    "    ax = f.add_subplot(gs[0, 1])\n",
    "    sinplot(6)\n",
    "\n",
    "with sns.axes_style(\"ticks\"):\n",
    "    ax = f.add_subplot(gs[1, 0])\n",
    "    sinplot(6)\n",
    "\n",
    "with sns.axes_style(\"whitegrid\"):\n",
    "    ax = f.add_subplot(gs[1, 1])\n",
    "    sinplot(6)\n",
    "    \n",
    "f.tight_layout()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Overriding elements of the seaborn styles\n",
    "-----------------------------------------\n",
    "\n",
    "If you want to customize the seaborn styles, you can pass a dictionary of parameters to the ``rc`` argument of :func:`axes_style` and :func:`set_style`. Note that you can only override the parameters that are part of the style definition through this method. (However, the higher-level :func:`set_theme` function takes a dictionary of any matplotlib parameters).\n",
    "\n",
    "If you want to see what parameters are included, you can just call the function with no arguments, which will return the current settings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.axes_style()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You can then set different versions of these parameters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_style(\"darkgrid\", {\"axes.facecolor\": \".9\"})\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ".. _plotting_context:\n",
    "\n",
    "Scaling plot elements\n",
    "---------------------\n",
    "\n",
    "A separate set of parameters control the scale of plot elements, which should let you use the same code to make plots that are suited for use in settings where larger or smaller plots are appropriate.\n",
    "\n",
    "First let's reset the default parameters by calling :func:`set_theme`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_theme()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The four preset contexts, in order of relative size, are ``paper``, ``notebook``, ``talk``, and ``poster``. The ``notebook`` style is the default, and was used in the plots above."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_context(\"paper\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_context(\"talk\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_context(\"poster\")\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Most of what you now know about the style functions should transfer to the context functions.\n",
    "\n",
    "You can call :func:`set_context` with one of these names to set the parameters, and you can override the parameters by providing a dictionary of parameter values.\n",
    "\n",
    "You can also independently scale the size of the font elements when changing the context. (This option is also available through the top-level :func:`set` function)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.set_context(\"notebook\", font_scale=1.5, rc={\"lines.linewidth\": 2.5})\n",
    "sinplot()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Similarly, you can temporarily control the scale of figures nested under a ``with`` statement.\n",
    "\n",
    "Both the style and the context can be quickly configured with the :func:`set` function. This function also sets the default color palette, but that will be covered in more detail in the :ref:`next section <palette_tutorial>` of the tutorial."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "py310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
