{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Residual plot" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.bistro.residual import *\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "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": 2, "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": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "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": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "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": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "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": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "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": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "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": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "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": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "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": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "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": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "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.8.15" } }, "nbformat": 4, "nbformat_minor": 4 }