{
"cells": [
{
"cell_type": "markdown",
"id": "6b06717c-7cf3-40d0-b5d8-919d65df7363",
"metadata": {},
"source": [
"# `ggbunch()` : Combining Plots with Custom Layout\n",
"\n",
"`ggbunch()` provides a flexible way to combine multiple plots into a single figure. \n",
"Unlike grid-based layouts, it allows precise positioning and sizing of each plot using relative coordinates. \n",
"You can place plots anywhere within the container, adjust their sizes individually,\n",
"and fine-tune their positions with pixel-level offsets."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6eeb62c1-2fc8-4513-a0c7-df0634f8b1db",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import pandas as pd\n",
"import geopandas as gpd\n",
"\n",
"from lets_plot import *\n",
"from lets_plot.geo_data import * # Geo-coding\n",
"\n",
"LetsPlot.setup_html()\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87813602-533d-46b2-a156-b57ac1362c5e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Unnamed: 0 | \n",
" Name | \n",
" Shape | \n",
" Elevation | \n",
" Last_Eruption | \n",
" Geo_Location | \n",
" Island | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0 | \n",
" Weh | \n",
" stratovolcano | \n",
" 617 metres (2,024 ft) | \n",
" Pleistocene | \n",
" 5.82°N 95.28°E | \n",
" Sumatra | \n",
"
\n",
" \n",
" | 1 | \n",
" 1 | \n",
" Seulawah Agam | \n",
" stratovolcano | \n",
" 1,810 metres (5,940 ft) | \n",
" 1839 (2) | \n",
" 5.448°N 95.658°E | \n",
" Sumatra | \n",
"
\n",
" \n",
" | 2 | \n",
" 2 | \n",
" Peuet Sague | \n",
" complex volcano | \n",
" 2,801 metres (9,190 ft) | \n",
" 25 December 2000 (2) | \n",
" 4.914°N 96.329°E | \n",
" Sumatra | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 Name Shape Elevation \\\n",
"0 0 Weh stratovolcano 617 metres (2,024 ft) \n",
"1 1 Seulawah Agam stratovolcano 1,810 metres (5,940 ft) \n",
"2 2 Peuet Sague complex volcano 2,801 metres (9,190 ft) \n",
"\n",
" Last_Eruption Geo_Location Island \n",
"0 Pleistocene 5.82°N 95.28°E Sumatra \n",
"1 1839 (2) 5.448°N 95.658°E Sumatra \n",
"2 25 December 2000 (2) 4.914°N 96.329°E Sumatra "
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_volc = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot/refs/heads/master/docs/f-25a/data/volcano_indo.csv\", encoding ='utf-8')\n",
"df_volc.head(3)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "47e72656-5c9f-41f9-ab62-364e31477760",
"metadata": {},
"outputs": [],
"source": [
"def dms_to_decimal(dms_string):\n",
" \"\"\"\n",
" Convert DMS coordinates to decimal degrees.\n",
" \n",
" >>> dms_to_decimal(\"99.539°E\")\n",
" 99.539\n",
" \n",
" >>> dms_to_decimal(\"99.539°W\")\n",
" -99.539\n",
" \n",
" \"\"\"\n",
" degrees, direction = dms_string.split('°')\n",
" degrees = float(degrees)\n",
" \n",
" # Adjusting the sign based on direction\n",
" if direction in ['S', 'W']:\n",
" degrees *= -1\n",
" \n",
" return degrees"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7f51b5e2-1831-4b89-9260-0d1e0042f358",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Unnamed: 0 | \n",
" Name | \n",
" Shape | \n",
" Elevation | \n",
" Last_Eruption | \n",
" Geo_Location | \n",
" Island | \n",
" geometry | \n",
"
\n",
" \n",
" \n",
" \n",
" | 0 | \n",
" 0 | \n",
" Weh | \n",
" stratovolcano | \n",
" 617 metres (2,024 ft) | \n",
" Pleistocene | \n",
" 5.82°N 95.28°E | \n",
" Sumatra | \n",
" POINT (95.28000 5.82000) | \n",
"
\n",
" \n",
" | 1 | \n",
" 1 | \n",
" Seulawah Agam | \n",
" stratovolcano | \n",
" 1,810 metres (5,940 ft) | \n",
" 1839 (2) | \n",
" 5.448°N 95.658°E | \n",
" Sumatra | \n",
" POINT (95.65800 5.44800) | \n",
"
\n",
" \n",
" | 2 | \n",
" 2 | \n",
" Peuet Sague | \n",
" complex volcano | \n",
" 2,801 metres (9,190 ft) | \n",
" 25 December 2000 (2) | \n",
" 4.914°N 96.329°E | \n",
" Sumatra | \n",
" POINT (96.32900 4.91400) | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Unnamed: 0 Name Shape Elevation \\\n",
"0 0 Weh stratovolcano 617 metres (2,024 ft) \n",
"1 1 Seulawah Agam stratovolcano 1,810 metres (5,940 ft) \n",
"2 2 Peuet Sague complex volcano 2,801 metres (9,190 ft) \n",
"\n",
" Last_Eruption Geo_Location Island \\\n",
"0 Pleistocene 5.82°N 95.28°E Sumatra \n",
"1 1839 (2) 5.448°N 95.658°E Sumatra \n",
"2 25 December 2000 (2) 4.914°N 96.329°E Sumatra \n",
"\n",
" geometry \n",
"0 POINT (95.28000 5.82000) \n",
"1 POINT (95.65800 5.44800) \n",
"2 POINT (96.32900 4.91400) "
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_volc = df_volc.dropna(subset=['Geo_Location'])\n",
"\n",
"# Split 'Geo_Location' str.\n",
"lat_lon_dms = df_volc.Geo_Location.str.split(' ').apply(lambda lst: lst[1:3])\n",
"\n",
"# Remove BOM symbols (\\ufeff) and\n",
"# convert DMS strings to decimal degrees.\n",
"lat_lon_dd = lat_lon_dms.apply(lambda lst: [dms_to_decimal(v.replace('\\ufeff', '')) for v in lst])\n",
"\n",
"# Create a Geodataframe by adding the 'geometry' column.\n",
"latitudes = lat_lon_dd.apply(lambda pair: pair[0])\n",
"longitudes = lat_lon_dd.apply(lambda pair: pair[1])\n",
"\n",
"gdf_volc = gpd.GeoDataFrame(df_volc, geometry=gpd.points_from_xy(longitudes, latitudes))\n",
"gdf_volc.head(3)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d9695eb0-f15c-4a76-97d2-df7b49679001",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Map of Indonesia\n",
"indonesia_gcoder = geocode_countries('Indonesia')\n",
"\n",
"indonesia_map = (ggplot() \n",
" + geom_map(map=indonesia_gcoder, size=0.5) \n",
" # The Equator\n",
" + geom_hline(yintercept=0, linetype=\"dotted\", tooltips='none')\n",
" # Volcanoes\n",
" + geom_point(data=gdf_volc, color='#fddbc7', tooltips=layer_tooltips()\n",
" .title(\"@Name\") \n",
" .line(\"Last Eruption | @Last_Eruption\"))\n",
" + theme_void() + theme(plot_inset=0)\n",
")\n",
"\n",
"indonesia_map + ggsize(800, 304)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "cd17b9a4-bb9b-4c6d-a7b9-176fe3fa2c3b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Map of Southeast Asia\n",
"southeast_asia_gcoder = geocode_countries(['Indonesia', \n",
" 'Australia', 'Malaysia', 'Thailand', 'Taiwan', 'Vietnam', \n",
" 'Philippines', 'Papua New Guinea', 'Palau',\n",
" 'Myanmar', 'Laos', 'Cambodia',\n",
" 'Indonesia', 'India', 'Bangladesh', 'China',\n",
" 'Bhutan', 'Nepal', 'Sri Lanka',\n",
" 'South Korea', 'North Korea', 'Japan'\n",
" ])\n",
"\n",
"southeast_asia_map = (ggplot() \n",
" + geom_map(map=southeast_asia_gcoder, fill='paper', size=0.1) \n",
" # The Equator\n",
" + geom_hline(yintercept=0, linetype=\"dotted\", tooltips='none')\n",
" # Indonesia bounding box \n",
" + geom_rect(map=indonesia_gcoder, size=0.2, fill='paper', alpha=0.3, color='red')\n",
" + theme_grey() + theme(axis='blank', panel_grid='blank')\n",
" + theme(plot_background=element_rect(size=1), plot_inset=0)\n",
")\n",
"\n",
"southeast_asia_map"
]
},
{
"cell_type": "markdown",
"id": "ca50ca41-4778-4f05-ad86-45c6337f4b61",
"metadata": {},
"source": [
"#### Combine Two Maps in One Figure Using `ggbunch()`"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "aaab4c15-c1ae-41e5-ba39-8c6a4cec20c7",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(ggbunch(\n",
" plots=[indonesia_map, southeast_asia_map],\n",
" regions=[(0, 0, 1, 1), # <- Indonesia map takes 100% of available space \n",
" (0.85, 0, 0.15, 0.53, # <- Southeast Asia inset map\n",
" -10, 0) # <- Offset in px for precise positioning\n",
" ]\n",
")\n",
" + theme(plot_background=element_rect(size=1), plot_inset=0,\n",
" plot_title=element_text(family='bold', size=20, hjust=0.5, color='#1a1a1a'))\n",
" + ggtitle('Indonesia Volcanic Activity')\n",
" + ggsize(800, 325)\n",
")"
]
}
],
"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.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}