{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Residual plot" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.454581Z", "iopub.status.busy": "2025-11-05T13:40:25.454455Z", "iopub.status.idle": "2025-11-05T13:40:25.458534Z", "shell.execute_reply": "2025-11-05T13:40:25.458355Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.bistro.residual import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.459428Z", "iopub.status.busy": "2025-11-05T13:40:25.459343Z", "iopub.status.idle": "2025-11-05T13:40:25.461286Z", "shell.execute_reply": "2025-11-05T13:40:25.461043Z" } }, "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:25.474050Z", "iopub.status.busy": "2025-11-05T13:40:25.473953Z", "iopub.status.idle": "2025-11-05T13:40:25.559906Z", "shell.execute_reply": "2025-11-05T13:40:25.559632Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(150, 5)\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa
\n", "
" ], "text/plain": [ " sepal_length sepal_width petal_length petal_width species\n", "0 5.1 3.5 1.4 0.2 setosa\n", "1 4.9 3.0 1.4 0.2 setosa\n", "2 4.7 3.2 1.3 0.2 setosa\n", "3 4.6 3.1 1.5 0.2 setosa\n", "4 5.0 3.6 1.4 0.2 setosa" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Default plot" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.560688Z", "iopub.status.busy": "2025-11-05T13:40:25.560616Z", "iopub.status.idle": "2025-11-05T13:40:25.858454Z", "shell.execute_reply": "2025-11-05T13:40:25.858188Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scatter plot (`method='none'`)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.859419Z", "iopub.status.busy": "2025-11-05T13:40:25.859294Z", "iopub.status.idle": "2025-11-05T13:40:25.863773Z", "shell.execute_reply": "2025-11-05T13:40:25.863564Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", method='none', hline=False, marginal='none') + theme_classic()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change geom type" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.864628Z", "iopub.status.busy": "2025-11-05T13:40:25.864556Z", "iopub.status.idle": "2025-11-05T13:40:25.870164Z", "shell.execute_reply": "2025-11-05T13:40:25.869961Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", geom='tile', marginal=\"hist:tr\") + \\\n", " ggsize(600, 200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Change method" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.870944Z", "iopub.status.busy": "2025-11-05T13:40:25.870874Z", "iopub.status.idle": "2025-11-05T13:40:25.882349Z", "shell.execute_reply": "2025-11-05T13:40:25.882128Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", method='loess', span=.25, max_n=25, seed=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geometries customization" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.883162Z", "iopub.status.busy": "2025-11-05T13:40:25.883091Z", "iopub.status.idle": "2025-11-05T13:40:25.893098Z", "shell.execute_reply": "2025-11-05T13:40:25.892895Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", size=5, color=\"#feb24c\", alpha=1/3)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.893862Z", "iopub.status.busy": "2025-11-05T13:40:25.893786Z", "iopub.status.idle": "2025-11-05T13:40:25.904937Z", "shell.execute_reply": "2025-11-05T13:40:25.904729Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", geom='none', color=\"black\", hline=False) + \\\n", " geom_hline(yintercept=0, color=\"black\") + \\\n", " geom_point(size=5, shape=21, color=\"black\", fill=\"#feb24c\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.905657Z", "iopub.status.busy": "2025-11-05T13:40:25.905578Z", "iopub.status.idle": "2025-11-05T13:40:25.911498Z", "shell.execute_reply": "2025-11-05T13:40:25.911297Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", hline=False, marginal='none') + \\\n", " geom_smooth(method='loess', se=True, level=.99, seed=42)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Marginal layers customization" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.912217Z", "iopub.status.busy": "2025-11-05T13:40:25.912144Z", "iopub.status.idle": "2025-11-05T13:40:25.930511Z", "shell.execute_reply": "2025-11-05T13:40:25.930279Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", marginal=\"box:lb:.03,hist:t:.4,hist:r\", color=\"black\") + \\\n", " ggmarginal(\"tr\", layer=geom_area(stat='density', color=\"magenta\", fill=\"magenta\", alpha=.1)) + \\\n", " theme_minimal()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grouping" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.931264Z", "iopub.status.busy": "2025-11-05T13:40:25.931185Z", "iopub.status.idle": "2025-11-05T13:40:25.953267Z", "shell.execute_reply": "2025-11-05T13:40:25.953005Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", color_by=\"species\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interaction with other layers" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.954069Z", "iopub.status.busy": "2025-11-05T13:40:25.953996Z", "iopub.status.idle": "2025-11-05T13:40:25.959709Z", "shell.execute_reply": "2025-11-05T13:40:25.959502Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", geom='tile', binwidth=[.4, .1], marginal='none') + \\\n", " coord_fixed(ratio=.25, flip=True) + \\\n", " theme(axis_ticks=\"blank\", axis_text=\"blank\", axis_line=\"blank\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:40:25.960393Z", "iopub.status.busy": "2025-11-05T13:40:25.960322Z", "iopub.status.idle": "2025-11-05T13:40:25.965377Z", "shell.execute_reply": "2025-11-05T13:40:25.965175Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "residual_plot(df, \"petal_length\", \"petal_width\", color=\"white\", binwidth=.1, marginal=\"hist:r\") + \\\n", " ylab(\"residual\") + \\\n", " theme_bw() + theme(text=element_text(family=\"monospace\")) + \\\n", " flavor_high_contrast_dark()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }