{
"cells": [
{
"cell_type": "markdown",
"id": "5a3851c8",
"metadata": {},
"source": [
"# Ridgeline plot"
]
},
{
"cell_type": "markdown",
"id": "86fe154a",
"metadata": {},
"source": [
"## Preparation"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "7dfbf1c6",
"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.mapping import as_discrete\n",
"LetsPlot.setup_html()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "bbcfcdc4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(150, 5)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sepal_length | \n",
" sepal_width | \n",
" petal_length | \n",
" petal_width | \n",
" species | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 5.1 | \n",
" 3.5 | \n",
" 1.4 | \n",
" 0.2 | \n",
" setosa | \n",
"
\n",
" \n",
" 1 | \n",
" 4.9 | \n",
" 3.0 | \n",
" 1.4 | \n",
" 0.2 | \n",
" setosa | \n",
"
\n",
" \n",
" 2 | \n",
" 4.7 | \n",
" 3.2 | \n",
" 1.3 | \n",
" 0.2 | \n",
" setosa | \n",
"
\n",
" \n",
" 3 | \n",
" 4.6 | \n",
" 3.1 | \n",
" 1.5 | \n",
" 0.2 | \n",
" setosa | \n",
"
\n",
" \n",
" 4 | \n",
" 5.0 | \n",
" 3.6 | \n",
" 1.4 | \n",
" 0.2 | \n",
" setosa | \n",
"
\n",
" \n",
"
\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": [
"iris_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/iris.csv\")\n",
"print(iris_df.shape)\n",
"iris_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "697027b8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(234, 12)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \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",
"
\n",
" \n",
" \n",
" \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",
"
\n",
" \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",
"
\n",
" \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",
"
\n",
" \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",
"
\n",
" \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",
"
\n",
" \n",
"
\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": [
"mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n",
"print(mpg_df.shape)\n",
"mpg_df.head()"
]
},
{
"cell_type": "markdown",
"id": "b59a2ac7",
"metadata": {},
"source": [
"## Plots"
]
},
{
"cell_type": "markdown",
"id": "8dd5f2e8",
"metadata": {},
"source": [
"### Default plot"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "bbb2109a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges()"
]
},
{
"cell_type": "markdown",
"id": "5d7a7f35",
"metadata": {},
"source": [
"### `min_height` parameter"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9dffec45",
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({\n",
" \"x\": [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7],\n",
" \"y\": [-.4, -.4, -.4, -.4, -.4, -.4, -.4, -.8, -.8, -.8, -.8, -.8, -.8, -.8],\n",
" \"h\": [.4, -.2, .6, -.8, .3, .1, .7, .1, .3, .1, -.6, -.1, -.3, -.1],\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "f540a0f4",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(df) + \\\n",
" geom_area_ridges(aes(\"x\", \"y\", height=\"h\"), stat='identity', color=\"black\", fill=\"#3182bd\", min_height=-.4)"
]
},
{
"cell_type": "markdown",
"id": "dfff5e9b",
"metadata": {},
"source": [
"### `trim` and `tails_cutoff` parameters"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ae081703",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"width, height = 400, 300\n",
"bunch = GGBunch()\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(trim=False, tails_cutoff=None) + \\\n",
" ggtitle(\"Default: trim=False, tails_cutoff=None\"),\n",
" 0, 0, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(trim=False, tails_cutoff=0) + \\\n",
" ggtitle(\"trim=False, tails_cutoff=0\"),\n",
" width, 0, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(trim=False, tails_cutoff=3) + \\\n",
" ggtitle(\"trim=False, tails_cutoff=3\"),\n",
" 0, height, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(trim=True) + \\\n",
" ggtitle(\"trim=True\"),\n",
" width, height, width, height)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "66e7aa25",
"metadata": {},
"source": [
"### `scale` parameter"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d5958892",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(scale=1.5)"
]
},
{
"cell_type": "markdown",
"id": "e4517238",
"metadata": {},
"source": [
"### Quantiles"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e7e17610",
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"quantiles = [.05, .25, .5, .75, .95]\n",
"width, height = 400, 300\n",
"bunch = GGBunch()\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(quantiles=quantiles, color='black'),\n",
" 0, 0, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(quantiles=quantiles, quantile_lines=True, color='black'),\n",
" width, 0, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(aes(fill=\"..quantile..\"), quantiles=quantiles, color='black', show_legend=False),\n",
" 0, height, width, height)\n",
"bunch.add_plot(ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(aes(fill=\"..quantile..\"), quantiles=quantiles, quantile_lines=True, color='black', show_legend=False),\n",
" width, height, width, height)\n",
"bunch.show()\n"
]
},
{
"cell_type": "markdown",
"id": "07ad0b8d",
"metadata": {},
"source": [
"### Other"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2c960426",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(iris_df, aes(\"sepal_length\", \"species\")) + \\\n",
" geom_area_ridges(kernel=\"triangular\", adjust=.8, quantile_lines=True, trim='all', \\\n",
" color=\"#993404\", fill=\"#fe9929\", tooltips=layer_tooltips().line(\"height|@..height..\")\\\n",
" .format(\"@..density..\", \".2f\").line(\"density|@..density..\")\\\n",
" .line(\"quantile|@..quantile..\"))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "aeabe120",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(mpg_df, aes(\"hwy\", as_discrete(\"year\"), fill=\"drv\")) + \\\n",
" geom_area_ridges(color=\"white\", alpha=.5) + \\\n",
" scale_fill_brewer(type='qual', palette='Set1') + \\\n",
" facet_grid(x=\"drv\") + \\\n",
" theme_bw() + flavor_darcula()"
]
}
],
"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": 5
}