{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ".. _function_tutorial:\n",
    "\n",
    ".. currentmodule:: seaborn"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Overview of seaborn plotting functions\n",
    "======================================\n",
    "\n",
    "Most of your interactions with seaborn will happen through a set of plotting functions. Later chapters in the tutorial will explore the specific features offered by each function. This chapter will introduce, at a high-level, the different kinds of functions that you will encounter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "from IPython.display import HTML\n",
    "sns.set_theme()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Similar functions for similar tasks\n",
    "-----------------------------------\n",
    "\n",
    "The seaborn namespace is flat; all of the functionality is accessible at the top level. But the code itself is hierarchically structured, with modules of functions that achieve similar visualization goals through different means. Most of the docs are structured around these modules: you'll encounter names like \"relational\", \"distributional\", and \"categorical\".\n",
    "\n",
    "For example, the :ref:`distributions module <distribution_api>` defines functions that specialize in representing the distribution of datapoints. This includes familiar methods like the histogram:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "penguins = sns.load_dataset(\"penguins\")\n",
    "sns.histplot(data=penguins, x=\"flipper_length_mm\", hue=\"species\", multiple=\"stack\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Along with similar, but perhaps less familiar, options such as kernel density estimation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.kdeplot(data=penguins, x=\"flipper_length_mm\", hue=\"species\", multiple=\"stack\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Functions within a module share a lot of underlying code and offer similar features that may not be present in other components of the library (such as ``multiple=\"stack\"`` in the examples above). They are designed to facilitate switching between different visual representations as you explore a dataset, because different representations often have complementary strengths and weaknesses."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Figure-level vs. axes-level functions\n",
    "-------------------------------------\n",
    "\n",
    "In addition to the different modules, there is a cross-cutting classification of seaborn functions as \"axes-level\" or \"figure-level\". The examples above are axes-level functions. They plot data onto a single :class:`matplotlib.pyplot.Axes` object, which is the return value of the function.\n",
    "\n",
    "In contrast, figure-level functions interface with matplotlib through a seaborn object, usually a :class:`FacetGrid`, that manages the figure. Each module has a single figure-level function, which offers a unitary interface to its various axes-level functions. The organization looks a bit like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [],
   "source": [
    "from matplotlib.patches import FancyBboxPatch\n",
    "\n",
    "f, ax = plt.subplots(figsize=(7, 5))\n",
    "f.subplots_adjust(0, 0, 1, 1)\n",
    "ax.set_axis_off()\n",
    "ax.set(xlim=(0, 1), ylim=(0, 1))\n",
    "\n",
    "\n",
    "modules = \"relational\", \"distributions\", \"categorical\"\n",
    "\n",
    "pal = sns.color_palette(\"deep\")\n",
    "colors = dict(relational=pal[0], distributions=pal[1], categorical=pal[2])\n",
    "\n",
    "pal = sns.color_palette(\"dark\")\n",
    "text_colors = dict(relational=pal[0], distributions=pal[1], categorical=pal[2])\n",
    "\n",
    "\n",
    "functions = dict(\n",
    "    relational=[\"scatterplot\", \"lineplot\"],\n",
    "    distributions=[\"histplot\", \"kdeplot\", \"ecdfplot\", \"rugplot\"],\n",
    "    categorical=[\"stripplot\", \"swarmplot\", \"boxplot\", \"violinplot\", \"pointplot\", \"barplot\"],\n",
    ")\n",
    "\n",
    "pad = .06\n",
    "\n",
    "w = .2\n",
    "h = .15\n",
    "\n",
    "xs = np.arange(0, 1,  1 / 3) + pad * 1.05\n",
    "y = .7\n",
    "\n",
    "for x, mod in zip(xs, modules):\n",
    "    color = colors[mod] + (.2,)\n",
    "    text_color = text_colors[mod]\n",
    "    box = FancyBboxPatch((x, y), w, h, f\"round,pad={pad}\", color=\"white\")\n",
    "    ax.add_artist(box)\n",
    "    box = FancyBboxPatch((x, y), w, h, f\"round,pad={pad}\", linewidth=1, edgecolor=text_color, facecolor=color)\n",
    "    ax.add_artist(box)\n",
    "    ax.text(x + w / 2, y + h / 2, f\"{mod[:3]}plot\\n({mod})\", ha=\"center\", va=\"center\", size=22, color=text_color)\n",
    "\n",
    "    for i, func in enumerate(functions[mod]):\n",
    "        x_i = x + w / 2\n",
    "        y_i =  y - i * .1 - h / 2 - pad\n",
    "        box = FancyBboxPatch((x_i - w / 2, y_i - pad / 3), w, h / 4, f\"round,pad={pad / 3}\",\n",
    "                              color=\"white\")\n",
    "        ax.add_artist(box)\n",
    "        box = FancyBboxPatch((x_i - w / 2, y_i - pad / 3), w, h / 4, f\"round,pad={pad / 3}\",\n",
    "                             linewidth=1, edgecolor=text_color, facecolor=color)\n",
    "        ax.add_artist(box)\n",
    "        ax.text(x_i, y_i, func, ha=\"center\", va=\"center\", size=18, color=text_color)\n",
    "\n",
    "    ax.plot([x_i, x_i], [y, y_i], zorder=-100, color=text_color, lw=1)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "For example, :func:`displot` is the figure-level function for the distributions module. Its default behavior is to draw a histogram, using the same code as :func:`histplot` behind the scenes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.displot(data=penguins, x=\"flipper_length_mm\", hue=\"species\", multiple=\"stack\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "To draw a kernel density plot instead, using the same code as :func:`kdeplot`, select it using the ``kind`` parameter:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.displot(data=penguins, x=\"flipper_length_mm\", hue=\"species\", multiple=\"stack\", kind=\"kde\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You'll notice that the figure-level plots look mostly like their axes-level counterparts, but there are a few differences. Notably, the legend is placed outside the plot. They also have a slightly different shape (more on that shortly).\n",
    "\n",
    "The most useful feature offered by the figure-level functions is that they can easily create figures with multiple subplots. For example, instead of stacking the three distributions for each species of penguins in the same axes, we can \"facet\" them by plotting each distribution across the columns of the figure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.displot(data=penguins, x=\"flipper_length_mm\", hue=\"species\", col=\"species\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The figure-level functions wrap their axes-level counterparts and pass the kind-specific keyword arguments (such as the bin size for a histogram) down to the underlying function. That means they are no less flexible, but there is a downside: the kind-specific parameters don't appear in the function signature or docstrings. Some of their features might be less discoverable, and you may need to look at two different pages of the documentation before understanding how to achieve a specific goal."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Axes-level functions make self-contained plots\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "\n",
    "The axes-level functions are written to act like drop-in replacements for matplotlib functions. While they add axis labels and legends automatically, they don't modify anything beyond the axes that they are drawn into. That means they can be composed into arbitrarily-complex matplotlib figures with predictable results.\n",
    "\n",
    "The axes-level functions call :func:`matplotlib.pyplot.gca` internally, which hooks into the matplotlib state-machine interface so that they draw their plots on the \"currently-active\" axes. But they additionally accept an ``ax=`` argument, which integrates with the object-oriented interface and lets you specify exactly where each plot should go:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f, axs = plt.subplots(1, 2, figsize=(8, 4), gridspec_kw=dict(width_ratios=[4, 3]))\n",
    "sns.scatterplot(data=penguins, x=\"flipper_length_mm\", y=\"bill_length_mm\", hue=\"species\", ax=axs[0])\n",
    "sns.histplot(data=penguins, x=\"species\", hue=\"species\", shrink=.8, alpha=.8, legend=False, ax=axs[1])\n",
    "f.tight_layout()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Figure-level functions own their figure\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "\n",
    "In contrast, figure-level functions cannot (easily) be composed with other plots. By design, they \"own\" their own figure, including its initialization, so there's no notion of using a figure-level function to draw a plot onto an existing axes. This constraint allows the figure-level functions to implement features such as putting the legend outside of the plot.\n",
    "\n",
    "Nevertheless, it is possible to go beyond what the figure-level functions offer by accessing the matplotlib axes on the object that they return and adding other elements to the plot that way:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tips = sns.load_dataset(\"tips\")\n",
    "g = sns.relplot(data=tips, x=\"total_bill\", y=\"tip\")\n",
    "g.ax.axline(xy1=(10, 2), slope=.2, color=\"b\", dashes=(5, 2))"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Customizing plots from a figure-level function\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "\n",
    "The figure-level functions return a :class:`FacetGrid` instance, which has a few methods for customizing attributes of the plot in a way that is \"smart\" about the subplot organization. For example, you can change the labels on the external axes using a single line of code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.relplot(data=penguins, x=\"flipper_length_mm\", y=\"bill_length_mm\", col=\"sex\")\n",
    "g.set_axis_labels(\"Flipper length (mm)\", \"Bill length (mm)\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "While convenient, this does add a bit of extra complexity, as you need to remember that this method is not part of the matplotlib API and exists only when using a figure-level function."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ".. _figure_size_tutorial:\n",
    "\n",
    "Specifying figure sizes\n",
    "^^^^^^^^^^^^^^^^^^^^^^^\n",
    "\n",
    "To increase or decrease the size of a matplotlib plot, you set the width and height of the entire figure, either in the `global rcParams <https://matplotlib.org/tutorials/introductory/customizing.html>`_, while setting up the plot (e.g. with the ``figsize`` parameter of :func:`matplotlib.pyplot.subplots`), or by calling a method on the figure object (e.g. :meth:`matplotlib.Figure.set_size_inches`). When using an axes-level function in seaborn, the same rules apply: the size of the plot is determined by the size of the figure it is part of and the axes layout in that figure.\n",
    "\n",
    "When using a figure-level function, there are several key differences. First, the functions themselves have parameters to control the figure size (although these are actually parameters of the underlying :class:`FacetGrid` that manages the figure). Second, these parameters, ``height`` and ``aspect``, parameterize the size slightly differently than the ``width``, ``height`` parameterization in matplotlib (using the seaborn parameters, ``width = height * aspect``). Most importantly, the parameters correspond to the size of each *subplot*, rather than the size of the overall figure.\n",
    "\n",
    "To illustrate the difference between these approaches, here is the default output of :func:`matplotlib.pyplot.subplots` with one subplot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f, ax = plt.subplots()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "A figure with multiple columns will have the same overall size, but the axes will be squeezed horizontally to fit in the space:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f, ax = plt.subplots(1, 2, sharey=True)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "In contrast, a plot created by a figure-level function will be square. To demonstrate that, let's set up an empty plot by using :class:`FacetGrid` directly. This happens behind the scenes in functions like :func:`relplot`, :func:`displot`, or :func:`catplot`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.FacetGrid(penguins)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "When additional columns are added, the figure itself will become wider, so that its subplots have the same size and shape:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.FacetGrid(penguins, col=\"sex\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "And you can adjust the size and shape of each subplot without accounting for the total number of rows and columns in the figure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.FacetGrid(penguins, col=\"sex\", height=3.5, aspect=.75)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The upshot is that you can assign faceting variables without stopping to think about how you'll need to adjust the total figure size. A downside is that, when you do want to change the figure size, you'll need to remember that things work a bit differently than they do in matplotlib."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Relative merits of figure-level functions\n",
    "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
    "\n",
    "Here is a summary of the pros and cons that we have discussed above:\n",
    "\n",
    ".. list-table::\n",
    "   :header-rows: 1\n",
    "\n",
    "   * - Advantages\n",
    "     - Drawbacks\n",
    "   * - Easy faceting by data variables\n",
    "     - Many parameters not in function signature\n",
    "   * - Legend outside of plot by default\n",
    "     - Cannot be part of a larger matplotlib figure\n",
    "   * - Easy figure-level customization\n",
    "     - Different API from matplotlib\n",
    "   * - Different figure size parameterization\n",
    "     - Different figure size parameterization\n",
    "\n",
    "On balance, the figure-level functions add some additional complexity that can make things more confusing for beginners, but their distinct features give them additional power. The tutorial documentation mostly uses the figure-level functions, because they produce slightly cleaner plots, and we generally recommend their use for most applications. The one situation where they are not a good choice is when you need to make a complex, standalone figure that composes multiple different plot kinds. At this point, it's recommended to set up the figure using matplotlib directly and to fill in the individual components using axes-level functions."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Combining multiple views on the data\n",
    "------------------------------------\n",
    "\n",
    "Two important plotting functions in seaborn don't fit cleanly into the classification scheme discussed above. These functions, :func:`jointplot` and :func:`pairplot`, employ multiple kinds of plots from different modules to represent multiple aspects of a dataset in a single figure. Both plots are figure-level functions and create figures with multiple subplots by default. But they use different objects to manage the figure: :class:`JointGrid` and :class:`PairGrid`, respectively.\n",
    "\n",
    ":func:`jointplot` plots the relationship or joint distribution of two variables while adding marginal axes that show the univariate distribution of each one separately:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.jointplot(data=penguins, x=\"flipper_length_mm\", y=\"bill_length_mm\", hue=\"species\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    ":func:`pairplot` is similar — it combines joint and marginal views — but rather than focusing on a single relationship, it visualizes every pairwise combination of variables simultaneously:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pairplot(data=penguins, hue=\"species\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Behind the scenes, these functions are using axes-level functions that you have already met (:func:`scatterplot` and :func:`kdeplot`), and they also have a ``kind`` parameter that lets you quickly swap in a different representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.jointplot(data=penguins, x=\"flipper_length_mm\", y=\"bill_length_mm\", hue=\"species\", kind=\"hist\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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
}
