{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "individual-occasions", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:35.971205Z", "iopub.status.busy": "2024-04-17T07:29:35.971121Z", "iopub.status.idle": "2024-04-17T07:29:36.319133Z", "shell.execute_reply": "2024-04-17T07:29:36.318680Z" } }, "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" ] } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot.geo_data import *\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "a74d1b5d-680a-4a06-9ba3-bd1dd269258d", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.320550Z", "iopub.status.busy": "2024-04-17T07:29:36.320342Z", "iopub.status.idle": "2024-04-17T07:29:36.322576Z", "shell.execute_reply": "2024-04-17T07:29:36.322406Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "pharmaceutical-classic", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.323502Z", "iopub.status.busy": "2024-04-17T07:29:36.323428Z", "iopub.status.idle": "2024-04-17T07:29:36.463374Z", "shell.execute_reply": "2024-04-17T07:29:36.462977Z" } }, "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", " \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
.......................................
229230volkswagenpassat2.020084auto(s6)f1928pmidsize
230231volkswagenpassat2.020084manual(m6)f2129pmidsize
231232volkswagenpassat2.819996auto(l5)f1626pmidsize
232233volkswagenpassat2.819996manual(m5)f1826pmidsize
233234volkswagenpassat3.620086auto(s6)f1726pmidsize
\n", "

234 rows × 12 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 \n", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 \n", ".. ... ... ... ... ... ... ... .. ... \n", "229 230 volkswagen passat 2.0 2008 4 auto(s6) f 19 \n", "230 231 volkswagen passat 2.0 2008 4 manual(m6) f 21 \n", "231 232 volkswagen passat 2.8 1999 6 auto(l5) f 16 \n", "232 233 volkswagen passat 2.8 1999 6 manual(m5) f 18 \n", "233 234 volkswagen passat 3.6 2008 6 auto(s6) f 17 \n", "\n", " hwy fl class \n", "0 29 p compact \n", "1 29 p compact \n", "2 31 p compact \n", "3 30 p compact \n", "4 26 p compact \n", ".. ... .. ... \n", "229 28 p midsize \n", "230 29 p midsize \n", "231 26 p midsize \n", "232 26 p midsize \n", "233 26 p midsize \n", "\n", "[234 rows x 12 columns]" ] }, "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", "mpg_df" ] }, { "cell_type": "code", "execution_count": 4, "id": "outdoor-chrome", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.464426Z", "iopub.status.busy": "2024-04-17T07:29:36.464348Z", "iopub.status.idle": "2024-04-17T07:29:36.466285Z", "shell.execute_reply": "2024-04-17T07:29:36.466103Z" } }, "outputs": [], "source": [ "p = (ggplot(mpg_df, aes(x='displ', y='cty', fill='drv', size='hwy'))\n", " + scale_size(range=[5, 15], breaks=[15, 40])\n", " + ggsize(600, 350)\n", " )" ] }, { "cell_type": "code", "execution_count": 5, "id": "impressed-belly", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.467248Z", "iopub.status.busy": "2024-04-17T07:29:36.467112Z", "iopub.status.idle": "2024-04-17T07:29:36.474614Z", "shell.execute_reply": "2024-04-17T07:29:36.474416Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tooltips: split a line using '\\n'\n", "\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips()\n", " .line('@manufacturer \\n@model')\n", " .line('@|@class')\n", " .line('@|@year'))" ] }, { "cell_type": "code", "execution_count": 6, "id": "driven-royal", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.475531Z", "iopub.status.busy": "2024-04-17T07:29:36.475457Z", "iopub.status.idle": "2024-04-17T07:29:36.479651Z", "shell.execute_reply": "2024-04-17T07:29:36.479484Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use '\\n' in 'format'\n", "\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips().format('^size', '{.0f} \\n(mpg)'))" ] }, { "cell_type": "code", "execution_count": 7, "id": "handed-challenge", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.480491Z", "iopub.status.busy": "2024-04-17T07:29:36.480414Z", "iopub.status.idle": "2024-04-17T07:29:36.484904Z", "shell.execute_reply": "2024-04-17T07:29:36.484734Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tooltip title\n", "\n", "# Add title to default tooltip lines\n", "\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips().title('@manufacturer @model'))" ] }, { "cell_type": "code", "execution_count": 8, "id": "coordinate-brooklyn", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.485706Z", "iopub.status.busy": "2024-04-17T07:29:36.485630Z", "iopub.status.idle": "2024-04-17T07:29:36.490179Z", "shell.execute_reply": "2024-04-17T07:29:36.490008Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add title to user specified multiline tooltip \n", "\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips(['class', 'year']).title('@manufacturer @model'))" ] }, { "cell_type": "code", "execution_count": 9, "id": "caring-cannon", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.491049Z", "iopub.status.busy": "2024-04-17T07:29:36.490896Z", "iopub.status.idle": "2024-04-17T07:29:36.495732Z", "shell.execute_reply": "2024-04-17T07:29:36.495559Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Multi-line title \n", "\n", "p + geom_point(shape=21,\n", " tooltips=layer_tooltips()\n", " .line('@|@class')\n", " .line('drive train|@drv')\n", " .line('@|@year')\n", " .title('Car info: \\n@manufacturer @model'))" ] }, { "cell_type": "code", "execution_count": 10, "id": "copyrighted-corpus", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.496626Z", "iopub.status.busy": "2024-04-17T07:29:36.496465Z", "iopub.status.idle": "2024-04-17T07:29:36.920609Z", "shell.execute_reply": "2024-04-17T07:29:36.920311Z" } }, "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", "
Unnamed: 0namelinkcoutry_idlatitudelongitudetext
00Algiers/wiki/Algiers136.7763893.058611— with nearly 3 million inhabitants, Algiers ...
11Annaba/wiki/Annaba136.9000007.766667— a town with 200,000 inhabitants in the east...
22Batna/wiki/Batna135.5500006.166667NaN
33Bechar/wiki/Bechar131.633333-2.200000— small city in the Sahara, not far from the ...
44Constantine/wiki/Constantine136.3650006.614722- Algeria's 3rd largest city with a canyon go...
\n", "
" ], "text/plain": [ " Unnamed: 0 name link coutry_id latitude \\\n", "0 0 Algiers /wiki/Algiers 1 36.776389 \n", "1 1 Annaba /wiki/Annaba 1 36.900000 \n", "2 2 Batna /wiki/Batna 1 35.550000 \n", "3 3 Bechar /wiki/Bechar 1 31.633333 \n", "4 4 Constantine /wiki/Constantine 1 36.365000 \n", "\n", " longitude text \n", "0 3.058611 — with nearly 3 million inhabitants, Algiers ... \n", "1 7.766667 — a town with 200,000 inhabitants in the east... \n", "2 6.166667 NaN \n", "3 -2.200000 — small city in the Sahara, not far from the ... \n", "4 6.614722 - Algeria's 3rd largest city with a canyon go... " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cities = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/cities.csv\")\n", "cities.head()" ] }, { "cell_type": "code", "execution_count": 11, "id": "marked-antarctica", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.921783Z", "iopub.status.busy": "2024-04-17T07:29:36.921698Z", "iopub.status.idle": "2024-04-17T07:29:36.925602Z", "shell.execute_reply": "2024-04-17T07:29:36.925428Z" } }, "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0namelinkcoutry_idlatitudelongitudetext
10440Prague/wiki/Prague14450.08333314.416667() — the capital and largest city of the Czec...
10451Brno/wiki/Brno14449.20000016.616667— the largest city in Moravia and its former ...
10462České Budějovice/wiki/%C4%8Cesk%C3%A9_Bud%C4%9Bjovice14448.97472214.474722– attractive large city in South Bohemia
10473Český Krumlov/wiki/%C4%8Cesk%C3%BD_Krumlov14448.81666714.316667— beautiful old town in South Bohemia with th...
10484Karlovy Vary/wiki/Karlovy_Vary14450.23333312.866667() — historic (and biggest Czech) spa resort,...
10495Kutná Hora/wiki/Kutn%C3%A1_Hora14449.95000015.266667— historical town with famous Saint Barbara c...
10506Olomouc/wiki/Olomouc14449.60000017.250000( or ) — riverside university town with a tho...
10517Ostrava/wiki/Ostrava14449.83555618.292500— a vibrant local subculture and long history...
10528Pilsen/wiki/Pilsen14449.74750013.377500() — home of the original Pilsner Urquell bee...
\n", "
" ], "text/plain": [ " Unnamed: 0 name link \\\n", "1044 0 Prague /wiki/Prague \n", "1045 1 Brno /wiki/Brno \n", "1046 2 České Budějovice /wiki/%C4%8Cesk%C3%A9_Bud%C4%9Bjovice \n", "1047 3 Český Krumlov /wiki/%C4%8Cesk%C3%BD_Krumlov \n", "1048 4 Karlovy Vary /wiki/Karlovy_Vary \n", "1049 5 Kutná Hora /wiki/Kutn%C3%A1_Hora \n", "1050 6 Olomouc /wiki/Olomouc \n", "1051 7 Ostrava /wiki/Ostrava \n", "1052 8 Pilsen /wiki/Pilsen \n", "\n", " coutry_id latitude longitude \\\n", "1044 144 50.083333 14.416667 \n", "1045 144 49.200000 16.616667 \n", "1046 144 48.974722 14.474722 \n", "1047 144 48.816667 14.316667 \n", "1048 144 50.233333 12.866667 \n", "1049 144 49.950000 15.266667 \n", "1050 144 49.600000 17.250000 \n", "1051 144 49.835556 18.292500 \n", "1052 144 49.747500 13.377500 \n", "\n", " text \n", "1044 () — the capital and largest city of the Czec... \n", "1045 — the largest city in Moravia and its former ... \n", "1046 – attractive large city in South Bohemia \n", "1047 — beautiful old town in South Bohemia with th... \n", "1048 () — historic (and biggest Czech) spa resort,... \n", "1049 — historical town with famous Saint Barbara c... \n", "1050 ( or ) — riverside university town with a tho... \n", "1051 — a vibrant local subculture and long history... \n", "1052 () — home of the original Pilsner Urquell bee... " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "czech_cities = cities.loc[cities['coutry_id'] == 144]\n", "czech_cities" ] }, { "cell_type": "code", "execution_count": 12, "id": "pacific-stick", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:36.926518Z", "iopub.status.busy": "2024-04-17T07:29:36.926442Z", "iopub.status.idle": "2024-04-17T07:29:37.150717Z", "shell.execute_reply": "2024-04-17T07:29:37.150154Z" } }, "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", "
idcountryfound namecentroidpositionlimit
051684CzechCzech Republic[15.5282667270321, 49.8037022352219][12.0905895531178, 48.5518079996109, 18.859253...[12.0905895531178, 48.5518079996109, 18.859253...
\n", "
" ], "text/plain": [ " id country found name centroid \\\n", "0 51684 Czech Czech Republic [15.5282667270321, 49.8037022352219] \n", "\n", " position \\\n", "0 [12.0905895531178, 48.5518079996109, 18.859253... \n", "\n", " limit \n", "0 [12.0905895531178, 48.5518079996109, 18.859253... " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "country_gcoder = geocode_countries(['Czech'])\n", "country_gcoder.get_geocodes()" ] }, { "cell_type": "code", "execution_count": 13, "id": "lightweight-integration", "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:29:37.151800Z", "iopub.status.busy": "2024-04-17T07:29:37.151696Z", "iopub.status.idle": "2024-04-17T07:29:37.388062Z", "shell.execute_reply": "2024-04-17T07:29:37.387640Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Automatic word wrap:\n", "# the value string is limited by the number of characters in it,\n", "# and description will be split into multiple lines.\n", "\n", "(ggplot()\n", " + geom_livemap(location=[15.78,49.35], zoom=6)\n", " + geom_polygon(aes(fill='country'), \n", " data=country_gcoder.inc_res().get_boundaries(), \n", " alpha=.2)\n", " + geom_point(aes('longitude', 'latitude', color='name'),\n", " data=czech_cities,\n", " size=4, \n", " tooltips=layer_tooltips()\n", " .title('@name')\n", " .line('Longitude|@longitude')\n", " .line('Latitude|@latitude')\n", " .line('Description|@text'))\n", " + theme(legend_position='none')\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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }