{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9252d5a5-8af1-4f99-b799-ee044329fb23",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn.objects as so\n",
    "from seaborn import load_dataset\n",
    "diamonds = load_dataset(\"diamonds\")\n",
    "mpg = load_dataset(\"mpg\").query(\"cylinders in [4, 6, 8]\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "bd43bcc6-b060-49c2-a429-8ea0ab046e2c",
   "metadata": {},
   "source": [
    "Passing the name of a function, such as `\"log\"` or `\"symlog\"` will set the scale's transform:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84b84cc1-ef1c-461e-b4af-4ce6e99886d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1 = so.Plot(diamonds, x=\"carat\", y=\"price\")\n",
    "p1.add(so.Dots()).scale(y=\"log\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b5ea9f7f-c776-48af-a4be-0053c3c12036",
   "metadata": {},
   "source": [
    "String arguments can also specify the the name of a palette that defines the output values (or \"range\") of the scale:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1f64d2f-6abd-48aa-9bab-c3e4614d0302",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1.add(so.Dots(), color=\"clarity\").scale(color=\"crest\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "37df8672-33b1-49a8-b702-a87c8b95db99",
   "metadata": {},
   "source": [
    "The scale's range can alternatively be specified as a tuple of min/max values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "371b8abd-ddfb-42f9-b730-f75b0e7b5fd6",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1.add(so.Dots(), pointsize=\"carat\").scale(pointsize=(2, 10))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "f0c4ead3-e950-48e4-9c81-c8734a8458d0",
   "metadata": {},
   "source": [
    "The tuple format can also be used for a color scale:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "678fd8b2-b031-4ec6-a567-a6711f722cbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "p1.add(so.Dots(), color=\"carat\").scale(color=(\".4\", \"#68d\"))"
   ]
  },
  {
   "cell_type": "raw",
   "id": "b6445ab7-2ec1-40be-95bc-9df0a5750bf5",
   "metadata": {},
   "source": [
    "For more control pass a scale object, such as :class:`Continuous`, which allows you to specify the input domain (`norm`), output range (`values`), and nonlinear transform (`trans`):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6a219ef-b50e-442e-82e9-8ae9e2cdb825",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "(\n",
    "    p1.add(so.Dots(), color=\"carat\")\n",
    "    .scale(color=so.Continuous((\".4\", \"#68d\"), norm=(1, 3), trans=\"sqrt\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "737e73a9-a0d5-4311-8c5c-4ca42f9194bf",
   "metadata": {
    "tags": []
   },
   "source": [
    "The scale objects also offer an interface for configuring the location of the scale ticks (including in the legend) and the formatting of the tick labels:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfaa426a-1a97-4b6f-91b6-ee378eabf194",
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    p1.add(so.Dots(), color=\"price\")\n",
    "    .scale(\n",
    "        x=so.Continuous(trans=\"sqrt\").tick(every=.5),\n",
    "        y=so.Continuous().label(like=\"${x:g}\"),\n",
    "        color=so.Continuous(\"ch:.2\").tick(upto=4).label(unit=\"\"),\n",
    "    )\n",
    "    .label(y=\"\")\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d4013795-fd5d-4a53-b145-e87f876a0684",
   "metadata": {},
   "source": [
    "If the scale includes a nonlinear transform, it will be applied *before* any statistical transforms:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9bf321f-c482-4d25-bb3b-7c499930b0d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "(\n",
    "    p1.add(so.Dots(color=\".7\"))\n",
    "    .add(so.Line(), so.PolyFit(order=2))\n",
    "    .scale(y=\"log\")\n",
    "    .limit(y=(250, 25000))\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "00ac5844-efb1-4683-a8ff-e864d0c68dff",
   "metadata": {},
   "source": [
    "The scale is also relevant for when numerical data should be treated as categories. Consider the following histogram:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04d5e6ae-30b2-495b-be1a-d99d6ffd4f44",
   "metadata": {},
   "outputs": [],
   "source": [
    "p2 = so.Plot(mpg, \"cylinders\").add(so.Bar(), so.Hist())\n",
    "p2"
   ]
  },
  {
   "cell_type": "raw",
   "id": "9b3dafad-aae0-4862-b1b2-bb76b75a9cec",
   "metadata": {},
   "source": [
    "By default, the plot gives `cylinders` a continuous scale, since it is a vector of floats. But assigning a :class:`Nominal` scale causes the histogram to bin observations properly:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f89331a-69fc-4714-adfb-0568690c1b66",
   "metadata": {},
   "outputs": [],
   "source": [
    "p2.scale(x=so.Nominal())"
   ]
  },
  {
   "cell_type": "raw",
   "id": "78880057-f4a7-40a1-a619-20d4b3be34dc",
   "metadata": {},
   "source": [
    "The default behavior for semantic mappings also depends on input data types and can be modified by the scale. Consider the sequential mapping applied to the colors in this plot:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "653abbc6-8227-48eb-9e1d-31587e6ef46d",
   "metadata": {},
   "outputs": [],
   "source": [
    "p3 = (\n",
    "    so.Plot(mpg, \"weight\", \"acceleration\", color=\"cylinders\")\n",
    "    .add(so.Dot(), marker=\"origin\")\n",
    ")\n",
    "p3"
   ]
  },
  {
   "cell_type": "raw",
   "id": "6ce5c9a8-5051-43b1-973c-fb9fb35ba399",
   "metadata": {},
   "source": [
    "Passing the name of a qualitative palette will select a :class:`Nominal` scale:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "218d6619-1fe3-4412-a2fc-efed4f542db7",
   "metadata": {},
   "outputs": [],
   "source": [
    "p3.scale(color=\"deep\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d2362247-6e0e-48fb-bbe4-2149f96785ae",
   "metadata": {},
   "source": [
    "A :class:`Nominal` scale is also implied when the output values are given as a list or dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bdf57da-cb05-4347-87ec-fac2c3763f12",
   "metadata": {},
   "outputs": [],
   "source": [
    "p3.scale(\n",
    "    color=[\"#49b\", \"#a6a\", \"#5b8\"],\n",
    "    marker={\"japan\": \".\", \"europe\": \"+\", \"usa\": \"*\"},\n",
    ")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "a7d92be7-9e96-4850-a26a-090c5ae9857b",
   "metadata": {},
   "source": [
    "Pass a :class:`Nominal` object directly to control the order of the category mappings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3c7eeb9-351f-484d-b0af-e18341569de3",
   "metadata": {},
   "outputs": [],
   "source": [
    "p3.scale(\n",
    "    color=so.Nominal([\"#008fd5\", \"#fc4f30\", \"#e5ae38\"]),\n",
    "    marker=so.Nominal(order=[\"japan\", \"europe\", \"usa\"])\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8885056-fd98-4964-a4a1-8c0344960409",
   "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
}
