{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "611aed40-d120-4fbf-b1e6-9712ed8167fc",
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import seaborn as sns\n",
    "sns.set_theme()\n",
    "mpg = sns.load_dataset(\"mpg\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "61bebade-0c45-4e99-9567-dfe0bc2dc6e1",
   "metadata": {},
   "source": [
    "Plot the relationship between two variables in a DataFrame:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f4107db-d89b-46ad-a4c6-9ba1181b2122",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"weight\", y=\"acceleration\")"
   ]
  },
  {
   "cell_type": "raw",
   "id": "146225d0-2e38-4b92-8e64-6d7f78311f40",
   "metadata": {},
   "source": [
    "Fit a higher-order polynomial regression to capture nonlinear trends:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba29488c-8a45-4387-bfb1-71a584fa1b3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"weight\", y=\"mpg\", order=2)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "0ad71f54-b362-465e-8780-1d8b99ff2d51",
   "metadata": {},
   "source": [
    "Alternatively, fit a log-linear regression:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aae2acaa-ed07-4568-97d2-8665603eb7eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"displacement\", y=\"mpg\", logx=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "eef37c8a-7190-465c-b963-076ec17e1b3a",
   "metadata": {},
   "source": [
    "Or use a locally-weighted (LOWESS) smoother:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9276c469-72ea-4c36-9b7c-19ecba564376",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"horsepower\", y=\"mpg\", lowess=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d18f1534-598e-4f08-91dd-0c4020f30b00",
   "metadata": {},
   "source": [
    "Fit a logistic regression when the response variable is binary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "79ec9180-10c9-4910-9713-dcd1fdd266be",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(x=mpg[\"weight\"], y=mpg[\"origin\"].eq(\"usa\").rename(\"from_usa\"), logistic=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "2e165783-d505-4acb-a20a-d22a49965c2b",
   "metadata": {},
   "source": [
    "Fit a robust regression to downweight the influence of outliers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fd5cf940-de8f-4230-8b04-5c650418f3c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"horsepower\", y=\"weight\", robust=True)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "e7d43c4e-e819-4634-8269-cbf5de4a2f24",
   "metadata": {},
   "source": [
    "Disable the confidence interval for faster plotting:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b21384ff-6395-4fa9-b7da-63e8a951d8a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"weight\", y=\"horsepower\", ci=None)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "06e979ac-f418-4ead-bde1-ec684d0545ff",
   "metadata": {},
   "source": [
    "Jitter the scatterplot when the `x` variable is discrete:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "543a8ace-a89e-4af9-bf6d-a8722ebdfac5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"cylinders\", y=\"weight\", x_jitter=.15)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "c3042eb2-0933-4886-9bff-88c276371516",
   "metadata": {},
   "source": [
    "Or aggregate over the distinct `x` values:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "158c6e36-8858-415b-b78c-7d8d79879ee5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"cylinders\", y=\"acceleration\", x_estimator=np.mean, order=2)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "d9cefe7a-7f86-4353-95da-d7e72e65d4fc",
   "metadata": {},
   "source": [
    "With a continuous `x` variable, bin and then aggregate:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c48829b-2e3b-4e6b-9b1d-5ba69f713617",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(data=mpg, x=\"weight\", y=\"mpg\", x_bins=np.arange(2000, 5500, 250), order=2)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "dfe5a36a-20b0-4e69-b986-fede8e1506cc",
   "metadata": {},
   "source": [
    "Customize the appearance of various elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df689a39-c5e1-4f7b-a8f9-8ffb09b95238",
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.regplot(\n",
    "    data=mpg, x=\"weight\", y=\"horsepower\",\n",
    "    ci=99, marker=\"x\", color=\".3\", line_kws=dict(color=\"r\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d625745b-3706-447b-9224-88e6cb1eb7f9",
   "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
}
