{
 "cells": [
  {
   "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 as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "sns.set_theme()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The ``flights`` dataset has 10 years of monthly airline passenger data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flights = sns.load_dataset(\"flights\")\n",
    "flights.head()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "To draw a line plot using long-form data, assign the ``x`` and ``y`` variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "may_flights = flights.query(\"month == 'May'\")\n",
    "sns.lineplot(data=may_flights, x=\"year\", y=\"passengers\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Pivot the dataframe to a wide-form representation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flights_wide = flights.pivot(index=\"year\", columns=\"month\", values=\"passengers\")\n",
    "flights_wide.head()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "To plot a single vector, pass it to ``data``. If the vector is a :class:`pandas.Series`, it will be plotted against its index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights_wide[\"May\"])"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Passing the entire wide-form dataset to ``data`` plots a separate line for each column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights_wide)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Passing the entire dataset in long-form mode will aggregate over repeated values (each year) to show the mean and 95% confidence interval:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights, x=\"year\", y=\"passengers\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Assign a grouping semantic (``hue``, ``size``, or ``style``) to plot separate lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights, x=\"year\", y=\"passengers\", hue=\"month\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The same column can be assigned to multiple semantic variables, which can increase the accessibility of the plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights, x=\"year\", y=\"passengers\", hue=\"month\", style=\"month\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Use the `orient` parameter to aggregate and sort along the vertical dimension of the plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=flights, x=\"passengers\", y=\"year\", orient=\"y\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Each semantic variable can also represent a different column. For that, we'll need a more complex dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fmri = sns.load_dataset(\"fmri\")\n",
    "fmri.head()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Repeated observations are aggregated even when semantic grouping is used:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=fmri, x=\"timepoint\", y=\"signal\", hue=\"event\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Assign both ``hue`` and ``style`` to represent two different grouping variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(data=fmri, x=\"timepoint\", y=\"signal\", hue=\"region\", style=\"event\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "When assigning a ``style`` variable, markers can be used instead of (or along with) dashes to distinguish the groups:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=fmri,\n",
    "    x=\"timepoint\", y=\"signal\", hue=\"event\", style=\"event\",\n",
    "    markers=True, dashes=False\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Show error bars instead of error bands and extend them to two standard error widths:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=fmri, x=\"timepoint\", y=\"signal\", hue=\"event\", err_style=\"bars\", errorbar=(\"se\", 2),\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Assigning the ``units`` variable will plot multiple lines without applying a semantic mapping:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=fmri.query(\"region == 'frontal'\"),\n",
    "    x=\"timepoint\", y=\"signal\", hue=\"event\", units=\"subject\",\n",
    "    estimator=None, lw=1,\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Load another dataset with a numeric grouping variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dots = sns.load_dataset(\"dots\").query(\"align == 'dots'\")\n",
    "dots.head()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Assigning a numeric variable to ``hue`` maps it differently, using a different default palette and a quantitative color mapping:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=dots, x=\"time\", y=\"firing_rate\", hue=\"coherence\", style=\"choice\",\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Control the color mapping by setting the ``palette`` and passing a :class:`matplotlib.colors.Normalize` object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=dots.query(\"coherence > 0\"),\n",
    "    x=\"time\", y=\"firing_rate\", hue=\"coherence\", style=\"choice\",\n",
    "     palette=\"flare\", hue_norm=mpl.colors.LogNorm(),\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Or pass specific colors, either as a Python list or dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "palette = sns.color_palette(\"mako_r\", 6)\n",
    "sns.lineplot(\n",
    "    data=dots, x=\"time\", y=\"firing_rate\",\n",
    "    hue=\"coherence\", style=\"choice\",\n",
    "    palette=palette\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Assign the ``size`` semantic to map the width of the lines with a numeric variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=dots, x=\"time\", y=\"firing_rate\",\n",
    "    size=\"coherence\", hue=\"choice\",\n",
    "    legend=\"full\"\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Pass a a tuple, ``sizes=(smallest, largest)``, to control the range of linewidths used to map the ``size`` semantic:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.lineplot(\n",
    "    data=dots, x=\"time\", y=\"firing_rate\",\n",
    "    size=\"coherence\", hue=\"choice\",\n",
    "    sizes=(.25, 2.5)\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "By default, the observations are sorted by ``x``. Disable this to plot a line with the order that observations appear in the dataset:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = np.random.normal(size=(2, 5000)).cumsum(axis=1)\n",
    "sns.lineplot(x=x, y=y, sort=False, lw=1)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Use :func:`relplot` to combine :func:`lineplot` and :class:`FacetGrid`. This allows grouping within additional categorical variables. Using :func:`relplot` is safer than using :class:`FacetGrid` directly, as it ensures synchronization of the semantic mappings across facets:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.relplot(\n",
    "    data=fmri, x=\"timepoint\", y=\"signal\",\n",
    "    col=\"region\", hue=\"event\", style=\"event\",\n",
    "    kind=\"line\"\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8bdfc9d9da1e36addfcfc8a3409187c45d33387af0f87d0d91e99e8d6403f1c3"
  },
  "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
}
