{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "hide"
    ]
   },
   "outputs": [],
   "source": [
    "import seaborn as sns\n",
    "sns.set_theme()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Calling the constructor initializes the figure, but it does not plot anything:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "penguins = sns.load_dataset(\"penguins\")\n",
    "sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The simplest plotting method, :meth:`JointGrid.plot` accepts a pair of functions (one for the joint axes and one for both marginal axes):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")\n",
    "g.plot(sns.scatterplot, sns.histplot)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The :meth:`JointGrid.plot` function also accepts additional keyword arguments, but it passes them to both functions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")\n",
    "g.plot(sns.scatterplot, sns.histplot, alpha=.7, edgecolor=\".2\", linewidth=.5)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "If you need to pass different keyword arguments to each function, you'll have to invoke :meth:`JointGrid.plot_joint` and :meth:`JointGrid.plot_marginals`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")\n",
    "g.plot_joint(sns.scatterplot, s=100, alpha=.5)\n",
    "g.plot_marginals(sns.histplot, kde=True)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You can also set up the grid without assigning any data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid()"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "You can then plot by accessing the ``ax_joint``, ``ax_marg_x``, and ``ax_marg_y`` attributes, which are :class:`matplotlib.axes.Axes` objects:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid()\n",
    "x, y = penguins[\"bill_length_mm\"], penguins[\"bill_depth_mm\"]\n",
    "sns.scatterplot(x=x, y=y, ec=\"b\", fc=\"none\", s=100, linewidth=1.5, ax=g.ax_joint)\n",
    "sns.histplot(x=x, fill=False, linewidth=2, ax=g.ax_marg_x)\n",
    "sns.kdeplot(y=y, linewidth=2, ax=g.ax_marg_y)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The plotting methods can use any seaborn functions that accept ``x`` and ``y`` variables:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")\n",
    "g.plot(sns.regplot, sns.boxplot)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "If the functions accept a ``hue`` variable, you can use it by assigning ``hue`` when you call the constructor:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\", hue=\"species\")\n",
    "g.plot(sns.scatterplot, sns.histplot)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Horizontal and/or vertical reference lines can be added to the joint and/or marginal axes using :meth:`JointGrid.refline`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = sns.JointGrid(data=penguins, x=\"bill_length_mm\", y=\"bill_depth_mm\")\n",
    "g.plot(sns.scatterplot, sns.histplot)\n",
    "g.refline(x=45, y=16)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "The figure will always be square (unless you resize it at the matplotlib layer), but its overall size and layout are configurable. The size is controlled by the ``height`` parameter. The relative ratio between the joint and marginal axes is controlled by ``ratio``, and the amount of space between the plots is controlled by ``space``:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.JointGrid(height=4, ratio=2, space=.05)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "By default, the ticks on the density axis of the marginal plots are turned off, but this is configurable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.JointGrid(marginal_ticks=True)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "Limits on the two data axes (which are shared across plots) can also be defined when setting up the figure:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sns.JointGrid(xlim=(-2, 5), ylim=(0, 10))"
   ]
  }
 ],
 "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": 4
}
