{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d053943-66c9-410d-ad65-ce91f1c1ff48",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn.objects as so\n",
    "from seaborn import load_dataset\n",
    "diamonds = load_dataset(\"diamonds\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "51b029af-b83b-4ae0-a6ff-f48bf9692518",
   "metadata": {},
   "source": [
    "The default behavior is to aggregate by taking a mean over each group:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28451b4e-9f4e-4604-b2b9-6138c4f51436",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = so.Plot(diamonds, \"clarity\", \"carat\")\n",
    "p.add(so.Bar(), so.Agg())"
   ]
  },
  {
   "cell_type": "raw",
   "id": "53859a3b-051c-423d-97ef-b03f647268b7",
   "metadata": {},
   "source": [
    "Other aggregation functions can be selected by name if they are pandas methods:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5beaac3a-b9f7-4acc-81c7-480599e3675e",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Bar(), so.Agg(\"median\"))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2d318ee3-56c1-4fd4-99a5-fa87db770f67",
   "metadata": {},
   "source": [
    "It's also possible to pass an arbitrary aggregation function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd11e289-7274-464a-b781-06fb756cf8de",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Bar(), so.Agg(lambda x: x.quantile(.75) - x.quantile(.25)))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "555394c1-25f8-4932-94d1-f67a8a9fa1c6",
   "metadata": {},
   "source": [
    "When other mapping variables are assigned, they'll be used to define aggregation groups. With some marks, it may be helpful to use additional transforms, such as :class:`Dodge`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5755cdeb-1d1a-4434-9cc5-91024735eb4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "p.add(so.Bar(), so.Agg(), so.Dodge(), color=\"cut\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "07eb1150-db57-4a58-b830-8a7aba9f46ec",
   "metadata": {},
   "source": [
    "The variable that gets aggregated depends on the orientation of the layer, which is usually inferred from the coordinate variable types (but may also be specified with the `orient` parameter in :meth:`Plot.add`):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bdcc970-1b6c-4a3d-b0bc-6c7a625163ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "so.Plot(diamonds, \"carat\", \"clarity\").add(so.Bar(), so.Agg())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad8006ff-5472-4345-9537-a5680c519f4f",
   "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
}
