{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Residual plot" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:28:10.277825Z", "iopub.status.busy": "2024-04-17T07:28:10.277642Z", "iopub.status.idle": "2024-04-17T07:28:10.591042Z", "shell.execute_reply": "2024-04-17T07:28:10.590736Z" } }, "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": "2024-04-17T07:28:10.592600Z", "iopub.status.busy": "2024-04-17T07:28:10.592341Z", "iopub.status.idle": "2024-04-17T07:28:10.594519Z", "shell.execute_reply": "2024-04-17T07:28:10.594279Z" } }, "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": "2024-04-17T07:28:10.607298Z", "iopub.status.busy": "2024-04-17T07:28:10.607134Z", "iopub.status.idle": "2024-04-17T07:28:10.740635Z", "shell.execute_reply": "2024-04-17T07:28:10.740240Z" } }, "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": "2024-04-17T07:28:10.741845Z", "iopub.status.busy": "2024-04-17T07:28:10.741753Z", "iopub.status.idle": "2024-04-17T07:28:11.047540Z", "shell.execute_reply": "2024-04-17T07:28:11.047325Z" } }, "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": "2024-04-17T07:28:11.048907Z", "iopub.status.busy": "2024-04-17T07:28:11.048788Z", "iopub.status.idle": "2024-04-17T07:28:11.052500Z", "shell.execute_reply": "2024-04-17T07:28:11.052264Z" } }, "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": "2024-04-17T07:28:11.053691Z", "iopub.status.busy": "2024-04-17T07:28:11.053542Z", "iopub.status.idle": "2024-04-17T07:28:11.058167Z", "shell.execute_reply": "2024-04-17T07:28:11.057998Z" } }, "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": "2024-04-17T07:28:11.059304Z", "iopub.status.busy": "2024-04-17T07:28:11.059227Z", "iopub.status.idle": "2024-04-17T07:28:11.070204Z", "shell.execute_reply": "2024-04-17T07:28:11.070026Z" } }, "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)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Geometries customization" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:28:11.071357Z", "iopub.status.busy": "2024-04-17T07:28:11.071281Z", "iopub.status.idle": "2024-04-17T07:28:11.080226Z", "shell.execute_reply": "2024-04-17T07:28:11.080054Z" } }, "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": "2024-04-17T07:28:11.081329Z", "iopub.status.busy": "2024-04-17T07:28:11.081239Z", "iopub.status.idle": "2024-04-17T07:28:11.090888Z", "shell.execute_reply": "2024-04-17T07:28:11.090706Z" } }, "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": "2024-04-17T07:28:11.092077Z", "iopub.status.busy": "2024-04-17T07:28:11.091985Z", "iopub.status.idle": "2024-04-17T07:28:11.097477Z", "shell.execute_reply": "2024-04-17T07:28:11.097292Z" } }, "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": "2024-04-17T07:28:11.098665Z", "iopub.status.busy": "2024-04-17T07:28:11.098479Z", "iopub.status.idle": "2024-04-17T07:28:11.116141Z", "shell.execute_reply": "2024-04-17T07:28:11.115910Z" } }, "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": "2024-04-17T07:28:11.117413Z", "iopub.status.busy": "2024-04-17T07:28:11.117236Z", "iopub.status.idle": "2024-04-17T07:28:11.138313Z", "shell.execute_reply": "2024-04-17T07:28:11.138048Z" } }, "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": "2024-04-17T07:28:11.139576Z", "iopub.status.busy": "2024-04-17T07:28:11.139482Z", "iopub.status.idle": "2024-04-17T07:28:11.144192Z", "shell.execute_reply": "2024-04-17T07:28:11.144002Z" } }, "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": "2024-04-17T07:28:11.145341Z", "iopub.status.busy": "2024-04-17T07:28:11.145211Z", "iopub.status.idle": "2024-04-17T07:28:11.149739Z", "shell.execute_reply": "2024-04-17T07:28:11.149549Z" } }, "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 }