{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Showing Quantiles on Density Plots" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2022-12-19T10:43:46.655506Z", "iopub.status.busy": "2022-12-19T10:43:46.654974Z", "iopub.status.idle": "2022-12-19T10:43:47.244118Z", "shell.execute_reply": "2022-12-19T10:43:47.244118Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\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", " \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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
45audia42.819996auto(l5)f1626pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Parameter `quantile_lines`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_args = {'color': \"black\", 'alpha': .75}\n", "p = ggplot(df, aes(\"hwy\", fill=\"drv\"))\n", "p1 = p + geom_density(**common_args) + \\\n", " ggtitle(\"geom_density(): quantile_lines=False (default)\")\n", "p2 = p + geom_density(quantile_lines=True, **common_args) + \\\n", " ggtitle(\"geom_density(): quantile_lines=True\")\n", "gggrid([p1, p2], ncol=1) + ggsize(700, 500)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"hwy\", \"drv\", fill=\"drv\"))\n", "p1 = p + geom_area_ridges() + \\\n", " ggtitle(\"geom_area_ridges(): quantile_lines=False (default)\")\n", "p2 = p + geom_area_ridges(quantile_lines=True) + \\\n", " ggtitle(\"geom_area_ridges(): quantile_lines=True\")\n", "gggrid([p1, p2])" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"drv\", \"hwy\", fill=\"drv\"))\n", "p1 = p + geom_violin() + \\\n", " ggtitle(\"geom_violin(): quantile_lines=False (default)\")\n", "p2 = p + geom_violin(quantile_lines=True) + \\\n", " ggtitle(\"geom_violin(): quantile_lines=True\")\n", "gggrid([p1, p2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Parameter `quantiles`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "quantiles = [0, .02, .3, .7, .98, 1]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_args = {'color': \"black\", 'alpha': .75, 'quantile_lines': True}\n", "p = ggplot(df, aes(\"hwy\", fill=\"drv\"))\n", "p1 = p + geom_density(**common_args) + \\\n", " ggtitle(\"geom_density(): quantiles=[.25, .5, .75] (default)\")\n", "p2 = p + geom_density(quantiles=quantiles, **common_args) + \\\n", " ggtitle(\"geom_density(): quantiles={0}\".format(quantiles))\n", "gggrid([p1, p2], ncol=1) + ggsize(700, 500)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"hwy\", \"drv\", fill=\"drv\"))\n", "p1 = p + geom_area_ridges(quantile_lines=True) + \\\n", " ggtitle(\"geom_area_ridges(): quantiles=[.25, .5, .75] (default)\")\n", "p2 = p + geom_area_ridges(quantile_lines=True, quantiles=quantiles) + \\\n", " ggtitle(\"geom_area_ridges(): quantiles={0}\".format(quantiles))\n", "gggrid([p1, p2])" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"drv\", \"hwy\", fill=\"drv\"))\n", "p1 = p + geom_violin(quantile_lines=True) + \\\n", " ggtitle(\"geom_violin(): quantiles=[.25, .5, .75] (default)\")\n", "p2 = p + geom_violin(quantile_lines=True, quantiles=quantiles) + \\\n", " ggtitle(\"geom_violin(): quantiles={0}\".format(quantiles))\n", "gggrid([p1, p2])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 3. Using `..quantile..` in Aesthetics Mapping" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "common_args = {'color': \"black\", 'alpha': .95}\n", "quantiles = np.linspace(0, 1, 15)\n", "fill_diverging = scale_fill_gradient2(low=\"white\", mid=\"#3F8F77\", high=\"white\", midpoint=0.5)\n", "\n", "p = ggplot(df, aes(\"hwy\", group=\"drv\"))\n", "p1 = p + geom_density(**common_args) + \\\n", " ggtitle(\"geom_density(): fill=None in aes (default)\")\n", "p2 = p + geom_density(aes(fill='..quantile..'), quantiles=quantiles, **common_args) + fill_diverging + \\\n", " ggtitle(\"geom_density(): fill='..quantile..' in aes\")\n", "gggrid([p1, p2], ncol=1) + ggsize(700, 500)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"hwy\", \"drv\"))\n", "p1 = p + geom_area_ridges() + \\\n", " ggtitle(\"geom_area_ridges(): fill=None in aes (default)\")\n", "p2 = p + geom_area_ridges(aes(fill='..quantile..')) + \\\n", " ggtitle(\"geom_area_ridges(): fill='..quantile..' in aes\")\n", "gggrid([p1, p2])" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(df, aes(\"drv\", \"hwy\"))\n", "p1 = p + geom_violin() + \\\n", " ggtitle(\"geom_violin(): fill=None in aes (default)\")\n", "p2 = p + geom_violin(aes(fill='..quantile..')) + \\\n", " ggtitle(\"geom_violin(): fill='..quantile..' in aes\")\n", "gggrid([p1, p2])" ] } ], "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 }