{
"cells": [
{
"cell_type": "markdown",
"id": "8717e97a",
"metadata": {},
"source": [
"# Variadic lines in `geom_path()` and `geom_line()`\n",
"\n",
"Using the `size` and `color` aesthetics in a mapping now produces lines with variadic width and color even within a particular group. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "0dbb2085",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:30.094073Z",
"iopub.status.busy": "2024-04-26T11:39:30.094073Z",
"iopub.status.idle": "2024-04-26T11:39:31.022914Z",
"shell.execute_reply": "2024-04-26T11:39:31.022914Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"from lets_plot import *\n",
"from lets_plot.mapping import as_discrete"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "951f6609",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.022914Z",
"iopub.status.busy": "2024-04-26T11:39:31.022914Z",
"iopub.status.idle": "2024-04-26T11:39:31.039000Z",
"shell.execute_reply": "2024-04-26T11:39:31.039000Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"LetsPlot.setup_html()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b2d14ae4",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.039000Z",
"iopub.status.busy": "2024-04-26T11:39:31.039000Z",
"iopub.status.idle": "2024-04-26T11:39:31.378343Z",
"shell.execute_reply": "2024-04-26T11:39:31.376868Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rownames | \n",
" Ozone | \n",
" Solar.R | \n",
" Wind | \n",
" Temp | \n",
" Month | \n",
" Day | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 41.0 | \n",
" 190.0 | \n",
" 7.4 | \n",
" 67 | \n",
" 5 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 36.0 | \n",
" 118.0 | \n",
" 8.0 | \n",
" 72 | \n",
" 5 | \n",
" 2 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 12.0 | \n",
" 149.0 | \n",
" 12.6 | \n",
" 74 | \n",
" 5 | \n",
" 3 | \n",
"
\n",
" \n",
" 3 | \n",
" 4 | \n",
" 18.0 | \n",
" 313.0 | \n",
" 11.5 | \n",
" 62 | \n",
" 5 | \n",
" 4 | \n",
"
\n",
" \n",
" 4 | \n",
" 5 | \n",
" NaN | \n",
" NaN | \n",
" 14.3 | \n",
" 56 | \n",
" 5 | \n",
" 5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rownames Ozone Solar.R Wind Temp Month Day\n",
"0 1 41.0 190.0 7.4 67 5 1\n",
"1 2 36.0 118.0 8.0 72 5 2\n",
"2 3 12.0 149.0 12.6 74 5 3\n",
"3 4 18.0 313.0 11.5 62 5 4\n",
"4 5 NaN NaN 14.3 56 5 5"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"airquality = pd.read_csv(\"https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/datasets/airquality.csv\")\n",
"airquality.head()"
]
},
{
"cell_type": "markdown",
"id": "32a850f2",
"metadata": {},
"source": [
"Let's use `size` mapping to visualize even more data on a plot:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "1abc12ec",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.383084Z",
"iopub.status.busy": "2024-04-26T11:39:31.383084Z",
"iopub.status.idle": "2024-04-26T11:39:31.469956Z",
"shell.execute_reply": "2024-04-26T11:39:31.468906Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(airquality) \\\n",
" + geom_line(aes(x = 'Day', y = 'Temp', size = 'Wind', color = as_discrete('Month'))) \\\n",
" + scale_size([0.5, 5.0]) \\\n",
" + ggsize(700, 500)"
]
},
{
"cell_type": "markdown",
"id": "8152607b",
"metadata": {},
"source": [
"`color` mapping can also be used to visualize more data on a single plot:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6d2b791b",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.472407Z",
"iopub.status.busy": "2024-04-26T11:39:31.472407Z",
"iopub.status.idle": "2024-04-26T11:39:31.484329Z",
"shell.execute_reply": "2024-04-26T11:39:31.483316Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(airquality) \\\n",
" + geom_line(aes(x = 'Day', y = 'Temp', color = 'Wind', group = 'Month'), size=2) \\\n",
" + scale_color_hue(h=[0, 150], h_start=240) \\\n",
" + ggsize(700, 500)"
]
},
{
"cell_type": "markdown",
"id": "e129d141",
"metadata": {},
"source": [
"Both `size` and `color` aesthetics can be used together to make a plot even more expressive:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "adfc369f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.487341Z",
"iopub.status.busy": "2024-04-26T11:39:31.486342Z",
"iopub.status.idle": "2024-04-26T11:39:31.498473Z",
"shell.execute_reply": "2024-04-26T11:39:31.498473Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(airquality) \\\n",
" + geom_line(aes(x = 'Day', y = 'Temp', size = 'Wind', color = 'Wind', group = 'Month')) \\\n",
" + scale_size([0.5, 6.0]) \\\n",
" + scale_color_hue(h=[0, 150], h_start=240) \\\n",
" + ggsize(700, 500)"
]
},
{
"cell_type": "markdown",
"id": "e5404512",
"metadata": {},
"source": [
"### Variadic lines and interactive maps\n",
"The map of Napoleon's Russian campaign \n",
"See: https://en.wikipedia.org/wiki/Charles_Joseph_Minard#The_map_of_Napoleon's_Russian_campaign"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "d59942ef",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.502367Z",
"iopub.status.busy": "2024-04-26T11:39:31.502367Z",
"iopub.status.idle": "2024-04-26T11:39:31.771136Z",
"shell.execute_reply": "2024-04-26T11:39:31.770130Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rownames | \n",
" long | \n",
" lat | \n",
" survivors | \n",
" direction | \n",
" group | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 24.0 | \n",
" 54.9 | \n",
" 340000 | \n",
" A | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 24.5 | \n",
" 55.0 | \n",
" 340000 | \n",
" A | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 25.5 | \n",
" 54.5 | \n",
" 340000 | \n",
" A | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" 4 | \n",
" 26.0 | \n",
" 54.7 | \n",
" 320000 | \n",
" A | \n",
" 1 | \n",
"
\n",
" \n",
" 4 | \n",
" 5 | \n",
" 27.0 | \n",
" 54.8 | \n",
" 300000 | \n",
" A | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rownames long lat survivors direction group\n",
"0 1 24.0 54.9 340000 A 1\n",
"1 2 24.5 55.0 340000 A 1\n",
"2 3 25.5 54.5 340000 A 1\n",
"3 4 26.0 54.7 320000 A 1\n",
"4 5 27.0 54.8 300000 A 1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"minard = pd.read_csv('https://raw.githubusercontent.com/vincentarelbundock/Rdatasets/master/csv/HistData/Minard.troops.csv')\n",
"minard.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d5b94934",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:39:31.774115Z",
"iopub.status.busy": "2024-04-26T11:39:31.774115Z",
"iopub.status.idle": "2024-04-26T11:39:31.797142Z",
"shell.execute_reply": "2024-04-26T11:39:31.795717Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ggplot(minard) \\\n",
" + geom_livemap() \\\n",
" + geom_path(aes(x='long', y='lat', size='survivors', group='group', color='direction')) \\\n",
" + scale_size([1.0, 20.0])"
]
}
],
"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": 5
}