{
"cells": [
{
"cell_type": "markdown",
"id": "06050c71",
"metadata": {},
"source": [
"# Coordinate Systems"
]
},
{
"cell_type": "markdown",
"id": "9fc49533",
"metadata": {},
"source": [
"## Preparation"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ded7e809",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:06.073833Z",
"iopub.status.busy": "2024-04-17T07:28:06.073612Z",
"iopub.status.idle": "2024-04-17T07:28:06.421982Z",
"shell.execute_reply": "2024-04-17T07:28:06.421776Z"
}
},
"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",
"\n",
"from lets_plot import *\n",
"from lets_plot.geo_data import *\n",
"LetsPlot.setup_html()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f45fec18",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:06.423248Z",
"iopub.status.busy": "2024-04-17T07:28:06.423129Z",
"iopub.status.idle": "2024-04-17T07:28:06.558977Z",
"shell.execute_reply": "2024-04-17T07:28:06.558592Z"
}
},
"outputs": [
{
"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": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mpg = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n",
"mpg.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "7ec3a720",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:06.560180Z",
"iopub.status.busy": "2024-04-17T07:28:06.560101Z",
"iopub.status.idle": "2024-04-17T07:28:07.148385Z",
"shell.execute_reply": "2024-04-17T07:28:07.148091Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" state | \n",
" found name | \n",
" geometry | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Wales | \n",
" Wales | \n",
" MULTIPOLYGON (((-5.35304 51.86591, -5.33064 51... | \n",
"
\n",
" \n",
" 1 | \n",
" Scotland | \n",
" Scotland | \n",
" MULTIPOLYGON (((-8.61762 57.82752, -8.56173 57... | \n",
"
\n",
" \n",
" 2 | \n",
" England | \n",
" England | \n",
" MULTIPOLYGON (((-6.33758 49.94591, -6.34859 49... | \n",
"
\n",
" \n",
" 3 | \n",
" Northern Ireland | \n",
" Northern Ireland | \n",
" MULTIPOLYGON (((-8.00601 54.54594, -7.85066 54... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" state found name \\\n",
"0 Wales Wales \n",
"1 Scotland Scotland \n",
"2 England England \n",
"3 Northern Ireland Northern Ireland \n",
"\n",
" geometry \n",
"0 MULTIPOLYGON (((-5.35304 51.86591, -5.33064 51... \n",
"1 MULTIPOLYGON (((-8.61762 57.82752, -8.56173 57... \n",
"2 MULTIPOLYGON (((-6.33758 49.94591, -6.34859 49... \n",
"3 MULTIPOLYGON (((-8.00601 54.54594, -7.85066 54... "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"states_gdf = geocode_states().scope(\"UK\").get_boundaries(6)\n",
"states_gdf.head()"
]
},
{
"cell_type": "markdown",
"id": "c521143d",
"metadata": {},
"source": [
"## Cartesian Coordinates\n",
"\n",
"Typical use case: changing limits of axes."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "a2bd521e",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.149573Z",
"iopub.status.busy": "2024-04-17T07:28:07.149501Z",
"iopub.status.idle": "2024-04-17T07:28:07.157757Z",
"shell.execute_reply": "2024-04-17T07:28:07.157580Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(\"cty\", \"hwy\")) + geom_point()\n",
"p1 = p + ggtitle(\"Default\")\n",
"p2 = p + coord_cartesian(xlim=[10, 30], ylim=[10, 40]) + ggtitle(\"With coord_cartesian()\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 500, 375)\n",
"bunch.add_plot(p2, 500, 0, 500, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "6c215104-0023-4de2-b557-c1e8eb6eb7a1",
"metadata": {},
"source": [
"## Polar Coordinates\n",
"\n",
"Typical use case: for pie charts and polar plots."
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "c6356683-4327-4d6d-bffe-b6715ea232cc",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.158949Z",
"iopub.status.busy": "2024-04-17T07:28:07.158854Z",
"iopub.status.idle": "2024-04-17T07:28:07.164990Z",
"shell.execute_reply": "2024-04-17T07:28:07.164676Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(\"cty\", \"hwy\")) + geom_point()\n",
"p1 = p + ggtitle(\"Default\")\n",
"p2 = p + coord_polar() + ggtitle(\"With coord_polar()\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 500, 375)\n",
"bunch.add_plot(p2, 500, 0, 500, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "92948a7e",
"metadata": {},
"source": [
"## Fixed Coordinates\n",
"\n",
"Typical use case: stretching a plot along one of the axes."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "84cb1010",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.166124Z",
"iopub.status.busy": "2024-04-17T07:28:07.166001Z",
"iopub.status.idle": "2024-04-17T07:28:07.171611Z",
"shell.execute_reply": "2024-04-17T07:28:07.171442Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(\"cty\", \"hwy\")) + geom_point()\n",
"p1 = p + ggtitle(\"Default\")\n",
"p2 = p + coord_fixed(ratio=1) + ggtitle(\"With coord_fixed()\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 500, 375)\n",
"bunch.add_plot(p2, 500, 0, 300, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "81b559c3",
"metadata": {},
"source": [
"Fixing coordinates could be combined with changing limits as for `coord_cartesian()`."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2851857d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.172643Z",
"iopub.status.busy": "2024-04-17T07:28:07.172532Z",
"iopub.status.idle": "2024-04-17T07:28:07.178493Z",
"shell.execute_reply": "2024-04-17T07:28:07.178317Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(\"cty\", \"hwy\")) + geom_point()\n",
"p1 = p + coord_fixed(ratio=1) + ggtitle(\"Stretching only\")\n",
"p2 = p + coord_fixed(ratio=1, xlim=[10, 30], ylim=[10, 40]) + ggtitle(\"With changing limits\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 300, 375)\n",
"bunch.add_plot(p2, 300, 0, 300, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "cf991252",
"metadata": {},
"source": [
"## Flipping Coordinates"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "25b84153",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.179464Z",
"iopub.status.busy": "2024-04-17T07:28:07.179348Z",
"iopub.status.idle": "2024-04-17T07:28:07.184797Z",
"shell.execute_reply": "2024-04-17T07:28:07.184624Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(x=\"fl\")) + geom_bar()\n",
"p1 = p + ggtitle(\"Default\")\n",
"p2 = p + coord_flip() + ggtitle(\"With coord_flip()\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 500, 375)\n",
"bunch.add_plot(p2, 500, 0, 300, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "ad0fe8d9",
"metadata": {},
"source": [
"Flipping could also be combined with changing limits."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "05a7d75d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.185722Z",
"iopub.status.busy": "2024-04-17T07:28:07.185609Z",
"iopub.status.idle": "2024-04-17T07:28:07.211457Z",
"shell.execute_reply": "2024-04-17T07:28:07.211139Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(x=\"fl\")) + geom_bar()\n",
"p1 = p + coord_flip() + ggtitle(\"Flipping only\")\n",
"p2 = p + coord_flip(ylim=[0, 300]) + ggtitle(\"With changing limits\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 300, 375)\n",
"bunch.add_plot(p2, 300, 0, 300, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "b7415cdb",
"metadata": {},
"source": [
"Also you can add flipping as an option to other coordinate systems."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "be2a6e7b",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.212716Z",
"iopub.status.busy": "2024-04-17T07:28:07.212629Z",
"iopub.status.idle": "2024-04-17T07:28:07.218597Z",
"shell.execute_reply": "2024-04-17T07:28:07.218411Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot(mpg, aes(x=\"fl\")) + geom_bar()\n",
"p1 = p + coord_flip() + ggtitle(\"Flipping through coord_flip()\")\n",
"p2 = p + coord_cartesian(flip=True) + ggtitle(\"Flipping through 'flip=True'\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 300, 375)\n",
"bunch.add_plot(p2, 300, 0, 300, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "82294706",
"metadata": {},
"source": [
"## Map Coordinates\n",
"\n",
"Typical use case: geospatial data."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "46cef4c3",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-17T07:28:07.219842Z",
"iopub.status.busy": "2024-04-17T07:28:07.219690Z",
"iopub.status.idle": "2024-04-17T07:28:07.261621Z",
"shell.execute_reply": "2024-04-17T07:28:07.261405Z"
}
},
"outputs": [
{
"data": {
"text/html": [
" \n",
" "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"p = ggplot() + geom_polygon(aes(fill=\"state\"), data=states_gdf)\n",
"p1 = p + ggtitle(\"Default\")\n",
"p2 = p + coord_map() + ggtitle(\"With coord_map()\")\n",
"\n",
"bunch = GGBunch()\n",
"bunch.add_plot(p1, 0, 0, 500, 375)\n",
"bunch.add_plot(p2, 500, 0, 500, 375)\n",
"bunch.show()"
]
},
{
"cell_type": "markdown",
"id": "41a12861",
"metadata": {},
"source": [
"Parameters `xlim`, `ylim`, `flip` are also available for `coord_map()`."
]
}
],
"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
}