{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Team colors\n",
    "\n",
    "`palette` and `team_colors` give you colors keyed by team, so a chart can be colored without hand-picking hex codes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sdvplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "\n",
    "df = pl.DataFrame({\"team\": [\"KC\", \"BUF\", \"PHI\", \"DET\"], \"epa\": [0.12, 0.10, 0.09, 0.15]})\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## A palette for seaborn\n",
    "\n",
    "`palette` accepts a polars Series of abbreviations, so it can be passed straight to seaborn."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {
    "sdvplot_gallery": {
     "alt": "Bar chart of EPA per play for KC, BUF, PHI and DET, each bar in its team's primary color.",
     "title": "Team colors in a seaborn bar chart"
    },
    "tags": [
     "gallery"
    ]
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "sns.barplot(\n",
    "    df.to_pandas(),\n",
    "    x=\"team\",\n",
    "    y=\"epa\",\n",
    "    hue=\"team\",\n",
    "    palette=sdvplot.palette(\"nfl\", teams=df[\"team\"]),\n",
    "    legend=False,\n",
    ")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "## One color per team\n",
    "\n",
    "`team_colors` returns one color per team, in the order given. `which=\"secondary\"` picks the secondary color."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdvplot.team_colors(df[\"team\"], \"nfl\", which=\"secondary\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "## Other libraries\n",
    "\n",
    "The palette is a plain dict of `team -> hex`, so other libraries take it directly (not executed here).\n",
    "\n",
    "Plotly:\n",
    "\n",
    "```python\n",
    "import plotly.express as px\n",
    "\n",
    "px.bar(df.to_pandas(), x=\"team\", y=\"epa\", color=\"team\",\n",
    "       color_discrete_map=sdvplot.palette(\"nfl\", teams=df[\"team\"]))\n",
    "```\n",
    "\n",
    "Altair:\n",
    "\n",
    "```python\n",
    "import altair as alt\n",
    "\n",
    "p = sdvplot.palette(\"nfl\", teams=df[\"team\"])\n",
    "alt.Chart(df.to_pandas()).mark_bar().encode(\n",
    "    x=\"team\", y=\"epa\", color=alt.Color(\"team\", scale=alt.Scale(domain=list(p), range=list(p.values()))),\n",
    ")\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "## Fallback colors\n",
    "\n",
    "Not every league has official colors. Where none exist, `color_source` is `\"fallback\"` and the color is one from a\n",
    "colorblind-safe categorical palette that only keeps teams distinguishable. Check `color_source` before treating a color as a team's own.\n",
    "Every OHL team below is a fallback; every NFL team has colors from nflverse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "sdvplot.teams(\"ohl\").select(\"team_id\", \"name\", \"color_primary\", \"color_source\").head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "sdvplot": {
   "description": "Color a chart by team with palette and team_colors, including leagues without official colors.",
   "label": "Team colors",
   "position": 2
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
