{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43f842ee-44c9-476b-ab08-112d23e2effb",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme(style=\"whitegrid\")\n",
    "penguins = sns.load_dataset(\"penguins\")\n",
    "flights = sns.load_dataset(\"flights\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f25d3647-9fad-47b2-b49d-db6f5b5c3795",
   "metadata": {},
   "source": [
    "Group by a categorical variable and plot aggregated values, with confidence intervals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a865fec-c034-4000-938d-b7cd89157495",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(data=penguins, x=\"island\", y=\"body_mass_g\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c65257ad-c87f-4b78-9b6c-cf792a691598",
   "metadata": {},
   "source": [
    "Add a second layer of grouping and differentiate with color:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f27011f1-0e3c-4dc4-818e-4a77930977b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(data=penguins, x=\"island\", y=\"body_mass_g\", hue=\"sex\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d51a887c-1f64-4ddf-af31-0476a983818b",
   "metadata": {},
   "source": [
    "Redundantly code the `hue` variable using the markers and linestyles for better accessibility:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bfb8bc1-6f9a-49a1-8b1d-6bcc992cb249",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(\n",
    "    data=penguins,\n",
    "    x=\"island\", y=\"body_mass_g\", hue=\"sex\",\n",
    "    markers=[\"o\", \"s\"], linestyles=[\"-\", \"--\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "44a11b7a-6847-4225-906e-58bbb56c6966",
   "metadata": {},
   "source": [
    "Use the error bars to represent the standard deviation of each distribution:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "386b25eb-7ab7-4a1d-9498-cef3e4fd3e6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(data=penguins, x=\"island\", y=\"body_mass_g\", errorbar=\"sd\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7490d4b8-d2ca-4cad-9ba3-5862aafb8165",
   "metadata": {},
   "source": [
    "Customize the appearance of the plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50b14810-2299-479c-b6c5-0fd10c4ed3de",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(\n",
    "    data=penguins, x=\"body_mass_g\", y=\"island\",\n",
    "    errorbar=(\"pi\", 100), capsize=.4,\n",
    "    color=\".5\", linestyle=\"none\", marker=\"D\",\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "479e4e0c-42c9-4d79-88eb-e397840a7e78",
   "metadata": {},
   "source": [
    "\"Dodge\" the artists along the categorical axis to reduce overplotting:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f94d069-c5f4-4579-a4bf-6d755962d48d",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(data=penguins, x=\"sex\", y=\"bill_depth_mm\", hue=\"species\", dodge=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "00273ada-cd12-410a-a268-38243d6514ae",
   "metadata": {},
   "source": [
    "Dodge by a specific amount, relative to the width allotted for each level:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94d6718d-2cfe-44f4-88e5-f47461d7d51f",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.stripplot(\n",
    "    data=penguins, x=\"species\", y=\"bill_depth_mm\", hue=\"sex\",\n",
    "    dodge=True, alpha=.2, legend=False,\n",
    ")\n",
    "sns.pointplot(\n",
    "    data=penguins, x=\"species\", y=\"bill_depth_mm\", hue=\"sex\",\n",
    "    dodge=.4, linestyle=\"none\", errorbar=None,\n",
    "    marker=\"_\", markersize=20, markeredgewidth=3,\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "e205e7c8-6b11-44e6-b43f-7416c427215d",
   "metadata": {},
   "source": [
    "When variables are not explicitly assigned and the dataset is two-dimensional, the plot will aggregate over each column:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e721e3b7-25c8-4e9c-a748-1c36b06d1100",
   "metadata": {},
   "outputs": [],
   "source": [
    "flights_wide = flights.pivot(index=\"year\", columns=\"month\", values=\"passengers\")\n",
    "sns.pointplot(flights_wide)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "0d2d7811-06e3-4882-86e3-225071c864f7",
   "metadata": {},
   "source": [
    "With one-dimensional data, each value is plotted (relative to its key or index when available):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acd91ddc-2a27-4b05-80fa-00ddcf1ae63e",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(flights_wide[\"Jun\"])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "573c2ba7-1e46-494d-9076-19b1c04b58c1",
   "metadata": {},
   "source": [
    "Control the formatting of the categorical variable as it appears in the tick labels:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7af6ab85-bfb1-42c2-8c68-2c91f22968d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.pointplot(flights_wide[\"Jun\"], formatter=lambda x: f\"'{x % 1900}\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c319e82e-1387-4c2b-8daf-3b7174cad180",
   "metadata": {},
   "source": [
    "Or preserve the native scale of the grouping variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e92e8af9-b734-4e4d-a240-7f3982fcfbcc",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = sns.pointplot(flights_wide[\"Jun\"], native_scale=True)\n",
    "ax.plot(1955, 335, marker=\"*\", color=\"r\", markersize=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f88be5c-7919-48cf-a84f-a5e6ac86e888",
   "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
}
