{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7edcf92f-6c11-4dc4-b684-118b3235d067",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme(style=\"whitegrid\")\n",
    "titanic = sns.load_dataset(\"titanic\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "4ca96805-333b-4186-9ad7-dcef4a9aacf5",
   "metadata": {},
   "source": [
    "Draw a single horizontal boxplot, assigning the data directly to the coordinate variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80532f2c-0f34-456c-9d5c-673682385461",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(x=titanic[\"age\"])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d9e33318-9595-4132-bfbd-8d88905fea79",
   "metadata": {},
   "source": [
    "Group by a categorical variable, referencing columns in a dataframe:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1e0a6a4-151d-42d7-a098-ec9b91f20906",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"age\", y=\"class\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d1e0d9e7-2d9b-49e3-8bb3-d97f2de7e733",
   "metadata": {},
   "source": [
    "Draw a vertical boxplot with nested grouping by two variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b8f74dc4-2b59-423a-90a7-dbf900c89251",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"class\", y=\"age\", hue=\"alive\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "59aaff3f-2bba-44d1-9901-2dd680bad3ad",
   "metadata": {},
   "source": [
    "Draw the boxes as line art and add a small gap between them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6af681be-c49e-4794-8a92-90c58ef330f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"class\", y=\"age\", hue=\"alive\", fill=False, gap=.1)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "db4ef9cb-0f0d-458b-a06d-c537c2b4d733",
   "metadata": {},
   "source": [
    "Cover the full range of the data with the whiskers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89aab45a-bc58-44e9-94ac-6a9aa0b20f5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"age\", y=\"deck\", whis=(0, 100))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "3844cc78-19a5-46e3-babd-77d6d7affcf0",
   "metadata": {},
   "source": [
    "Draw narrower boxes:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "399825eb-698a-4464-8a04-505b6bf7edc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"age\", y=\"deck\", width=.5)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "eaf35104-022d-4a20-9b60-f8b24acc7471",
   "metadata": {},
   "source": [
    "Modify the color and width of all the line artists:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e9dcaa3-b497-480e-b134-d31e01a7d4c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(data=titanic, x=\"age\", y=\"deck\", color=\".8\", linecolor=\"#137\", linewidth=.75)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a188c80-d69f-4a07-9b0d-ca467d2be680",
   "metadata": {},
   "source": [
    "Group by a numeric variable and preserve its native scaling:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d73c63f-58a8-4659-96fd-964493ba3a50",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = sns.boxplot(x=titanic[\"age\"].round(-1), y=titanic[\"fare\"], native_scale=True)\n",
    "ax.axvline(25, color=\".3\", dashes=(2, 2))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "28536179-8400-462d-bf3e-3d9f353fe03b",
   "metadata": {},
   "source": [
    "Customize the plot using parameters of the underlying matplotlib function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66c81b6e-e7fb-46c5-aa7b-f001241569b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.boxplot(\n",
    "    data=titanic, x=\"age\", y=\"class\",\n",
    "    notch=True, showcaps=False,\n",
    "    flierprops={\"marker\": \"x\"},\n",
    "    boxprops={\"facecolor\": (.3, .5, .7, .5)},\n",
    "    medianprops={\"color\": \"r\", \"linewidth\": 2},\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d2bb11b-0f4a-4efe-b18b-be34ebf24e49",
   "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": 5
}
