{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a6d582b-08c2-4fed-be56-afa1b986943a",
   "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": "b53b65b8-5670-4905-aa39-36db04f4b813",
   "metadata": {},
   "source": [
    "With long data, assign `x` and `y` to group by a categorical variable and plot aggregated values, with confidence intervals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f5c3ece-6295-4933-8a87-e80cd604c089",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(penguins, x=\"island\", y=\"body_mass_g\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "ed061d6f-bd3b-4189-bbc7-aed998be05cb",
   "metadata": {},
   "source": [
    "Prior to v0.13.0, each bar would have a different color. To replicate this behavior, assign the grouping variable to `hue` as well:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ded2e23-c610-450b-bcd2-1d2ba54db566",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(penguins, x=\"body_mass_g\", y=\"island\", hue=\"island\", legend=False)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "e00fa127-4dd4-4565-9897-51317adfea3c",
   "metadata": {},
   "source": [
    "When plotting a \"wide-form\" dataframe, each column will be aggregated and represented as a bar:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae7e0f4e-471e-4dee-8913-5e7b67e0a381",
   "metadata": {},
   "outputs": [],
   "source": [
    "flights_wide = flights.pivot(index=\"year\", columns=\"month\", values=\"passengers\")\n",
    "sns.barplot(flights_wide)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6020404c-15c6-4c00-9ffd-6c12ba624e52",
   "metadata": {},
   "source": [
    "Passing only a series (or dict) will plot each of its values, using the index (or keys) to label the bars:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77b2c3eb-c3e4-4d44-929a-27a456da4b88",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(flights_wide[\"Jun\"])"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b0c3b101-7649-4014-9ab2-10ff206d39d7",
   "metadata": {},
   "source": [
    "With long-form data, you can add a second layer of grouping with `hue`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac1a28d1-b3bd-4158-86d0-3defc12f8566",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(penguins, x=\"island\", y=\"body_mass_g\", hue=\"sex\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "069ce509-ee0d-42c8-b053-1b4b6d764449",
   "metadata": {},
   "source": [
    "Use the error bars to show the standard deviation rather than a confidence interval:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10445b78-a74a-4f14-a28b-a9164e592ae4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(penguins, x=\"island\", y=\"body_mass_g\", errorbar=\"sd\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6dc3d564-4d26-4753-a2a0-6194b10452bc",
   "metadata": {},
   "source": [
    "Use a different aggregation function and disable the error bars:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "448ba05e-c533-459d-84b6-0fca80e6e3ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(flights, x=\"year\", y=\"passengers\", estimator=\"sum\", errorbar=None)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "7746220d-b6b4-4ee5-886c-5867db35d4e3",
   "metadata": {},
   "source": [
    "Add text labels with each bar's value:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e343485c-636e-4b96-b20d-59a7f7155be8",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = sns.barplot(flights, x=\"year\", y=\"passengers\", estimator=\"sum\", errorbar=None)\n",
    "ax.bar_label(ax.containers[0], fontsize=10);"
   ]
  },
  {
   "cell_type": "raw",
   "id": "457702c2-9fa6-4021-a19b-f44b39aa0a19",
   "metadata": {},
   "source": [
    "Preserve the original scaling of the grouping variable and add annotations in numeric coordinates:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08b60118-5830-4fd7-8a66-431c065d57cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = sns.barplot(\n",
    "    flights, x=\"year\", y=\"passengers\",\n",
    "    native_scale=True,\n",
    "    estimator=\"sum\", errorbar=None,\n",
    ")\n",
    "ax.plot(1955, 3600, \"*\", markersize=10, color=\"r\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "206be839-f33b-4ffe-8101-bd98bc5942b8",
   "metadata": {},
   "source": [
    "Use `orient` to resolve ambiguity about which variable should group when both are numeric:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3aff3c69-3c24-40ad-af12-a507e33f5d3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(flights, x=\"passengers\", y=\"year\", orient=\"y\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "90277a3b-1f86-4884-97ad-e5d65df408ef",
   "metadata": {},
   "source": [
    "Customize the appearance of the plot using :class:`matplotlib.patches.Rectangle` and :class:`matplotlib.lines.Line2D` keyword arguments:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6f9ac1c-a77d-4ee3-bc5e-fec2071b33df",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.barplot(\n",
    "    penguins, x=\"body_mass_g\", y=\"island\",\n",
    "    errorbar=(\"pi\", 50), capsize=.4,\n",
    "    err_kws={\"color\": \".5\", \"linewidth\": 2.5},\n",
    "    linewidth=2.5, edgecolor=\".5\", facecolor=(0, 0, 0, 0),\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "08ef562f-13a3-4da5-a9cf-46deaa543890",
   "metadata": {},
   "source": [
    "Use :func:`catplot` to draw faceted bars, which is recommended over working directly with :class:`FacetGrid`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4d23777f-8a69-4c68-ab35-3e6740c61bcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.catplot(\n",
    "    penguins, kind=\"bar\",\n",
    "    x=\"sex\", y=\"body_mass_g\", col=\"species\",\n",
    "    height=4, aspect=.5,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0b6a62b9-eef7-4c85-a1c2-85a58231e6c6",
   "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
}
