{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Marginal Plots\n", "\n", "A marginal plot is a scatterplot (sometimes a density plot or other bivariate plot) that has histograms, boxplots, or other distribution visualization layers in the margins of the x- and y-axes. \n", "\n", "It allows studying the relationship between 2 numeric variables. \n", "\n", "You can use the `ggmarginal()` function to add marginal layers to a plot." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:05.371207Z", "iopub.status.busy": "2025-11-05T13:40:05.371123Z", "iopub.status.idle": "2025-11-05T13:40:05.374204Z", "shell.execute_reply": "2025-11-05T13:40:05.374014Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:05.375100Z", "iopub.status.busy": "2025-11-05T13:40:05.375029Z", "iopub.status.idle": "2025-11-05T13:40:05.376901Z", "shell.execute_reply": "2025-11-05T13:40:05.376586Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:05.389918Z", "iopub.status.busy": "2025-11-05T13:40:05.389818Z", "iopub.status.idle": "2025-11-05T13:40:05.391544Z", "shell.execute_reply": "2025-11-05T13:40:05.391248Z" } }, "outputs": [], "source": [ "# The default plot theme and palette.\n", "LetsPlot.set_theme(theme_light())\n", "palette = scale_color_manual(values=[\"#394449\", \"#F7C443\"]) + scale_fill_manual(values=[\"#394449\", \"#F7C443\"])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:05.392195Z", "iopub.status.busy": "2025-11-05T13:40:05.392124Z", "iopub.status.idle": "2025-11-05T13:40:05.394365Z", "shell.execute_reply": "2025-11-05T13:40:05.394173Z" } }, "outputs": [], "source": [ "np.random.seed(0)\n", "\n", "cov0 = [[1, -.8],\n", " [-.8, 1]]\n", "cov1 = [[10, .1],\n", " [.1, .1]]\n", "\n", "x0, y0 = np.random.multivariate_normal(mean=[-2, 0], cov=cov0, size=200).T\n", "x1, y1 = np.random.multivariate_normal(mean=[0, 1], cov=cov1, size=200).T\n", "\n", "data = dict(\n", " x=np.concatenate((x0, x1)),\n", " y=np.concatenate((y0, y1)),\n", " c=[\"A\"] * 200 + [\"B\"] * 200\n", ")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:05.394957Z", "iopub.status.busy": "2025-11-05T13:40:05.394885Z", "iopub.status.idle": "2025-11-05T13:40:05.425451Z", "shell.execute_reply": "2025-11-05T13:40:05.425245Z" } }, "outputs": [ { "data": { "text/html": [ " \n", " " ], "text/plain": [ "| \n", " | Unnamed: 0 | \n", "manufacturer | \n", "model | \n", "displ | \n", "year | \n", "cyl | \n", "trans | \n", "drv | \n", "cty | \n", "hwy | \n", "fl | \n", "class | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "auto(l5) | \n", "f | \n", "18 | \n", "29 | \n", "p | \n", "compact | \n", "
| 1 | \n", "2 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "manual(m5) | \n", "f | \n", "21 | \n", "29 | \n", "p | \n", "compact | \n", "
| 2 | \n", "3 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "manual(m6) | \n", "f | \n", "20 | \n", "31 | \n", "p | \n", "compact | \n", "
| 3 | \n", "4 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "auto(av) | \n", "f | \n", "21 | \n", "30 | \n", "p | \n", "compact | \n", "
| 4 | \n", "5 | \n", "audi | \n", "a4 | \n", "2.8 | \n", "1999 | \n", "6 | \n", "auto(l5) | \n", "f | \n", "16 | \n", "26 | \n", "p | \n", "compact | \n", "