{ "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", " \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": 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", " \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", "
statefound namegeometry
0WalesWalesMULTIPOLYGON (((-5.35304 51.86591, -5.33064 51...
1ScotlandScotlandMULTIPOLYGON (((-8.61762 57.82752, -8.56173 57...
2EnglandEnglandMULTIPOLYGON (((-6.33758 49.94591, -6.34859 49...
3Northern IrelandNorthern IrelandMULTIPOLYGON (((-8.00601 54.54594, -7.85066 54...
\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 }