{ "cells": [ { "cell_type": "markdown", "id": "e3d02a8a-ca67-408f-9fed-fb626895939b", "metadata": {}, "source": [ "# Zooming" ] }, { "cell_type": "code", "execution_count": 1, "id": "6c074779-802c-4e1a-9afe-be1be62f1a64", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:55.997118Z", "iopub.status.busy": "2024-08-23T10:32:55.997027Z", "iopub.status.idle": "2024-08-23T10:32:56.350335Z", "shell.execute_reply": "2024-08-23T10:32:56.349883Z" } }, "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 import *\n", "from lets_plot.geo_data import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "f56358c4-c485-4929-9c35-3410211d3269", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:56.352223Z", "iopub.status.busy": "2024-08-23T10:32:56.352107Z", "iopub.status.idle": "2024-08-23T10:32:56.354082Z", "shell.execute_reply": "2024-08-23T10:32:56.353915Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "85c71bba-c190-4666-be10-53700a376bed", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:56.355508Z", "iopub.status.busy": "2024-08-23T10:32:56.355364Z", "iopub.status.idle": "2024-08-23T10:32:57.412033Z", "shell.execute_reply": "2024-08-23T10:32:57.411709Z" } }, "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", "
statefound namegeometry
0ILIllinoisMULTIPOLYGON (((-89.13301 36.98200, -89.16777 ...
1INIndianaMULTIPOLYGON (((-84.81993 39.10544, -84.83405 ...
2MIMichiganMULTIPOLYGON (((-90.41862 46.56636, -90.00014 ...
3OHOhioMULTIPOLYGON (((-82.59342 38.42186, -82.60076 ...
4WIWisconsinMULTIPOLYGON (((-91.21771 43.50055, -91.21829 ...
\n", "
" ], "text/plain": [ " state found name geometry\n", "0 IL Illinois MULTIPOLYGON (((-89.13301 36.98200, -89.16777 ...\n", "1 IN Indiana MULTIPOLYGON (((-84.81993 39.10544, -84.83405 ...\n", "2 MI Michigan MULTIPOLYGON (((-90.41862 46.56636, -90.00014 ...\n", "3 OH Ohio MULTIPOLYGON (((-82.59342 38.42186, -82.60076 ...\n", "4 WI Wisconsin MULTIPOLYGON (((-91.21771 43.50055, -91.21829 ..." ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/midwest.csv')\n", "states = geocode('state', df.state.unique(), scope='US').get_boundaries(9)\n", "states.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "5febf216-916c-4a5e-a41a-9506f1f713b4", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:57.413740Z", "iopub.status.busy": "2024-08-23T10:32:57.413651Z", "iopub.status.idle": "2024-08-23T10:32:57.416598Z", "shell.execute_reply": "2024-08-23T10:32:57.416409Z" } }, "outputs": [], "source": [ "p = ggplot() + geom_map(data=states, tooltips=layer_tooltips().line('@{found name}')) + theme_void()" ] }, { "cell_type": "markdown", "id": "873a2ddd-bf81-41cf-af78-ef6c7ec17031", "metadata": {}, "source": [ "### Zooming without clipping\n", "\n", "Preferred type of zooming." ] }, { "cell_type": "code", "execution_count": 5, "id": "fd78a154-9929-42e3-890f-757390ca41d8", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:57.417679Z", "iopub.status.busy": "2024-08-23T10:32:57.417556Z", "iopub.status.idle": "2024-08-23T10:32:57.445667Z", "shell.execute_reply": "2024-08-23T10:32:57.445462Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p1 = p + ggtitle(\"Default\")\n", "p2 = p + coord_map(ylim=[36, 43]) + ggtitle(\"Zoom with coord_map()\")\n", "\n", "w, h = 500, 375\n", "bunch = GGBunch()\n", "bunch.add_plot(p1, 0, 0, w, h)\n", "bunch.add_plot(p2, w, 0, w, h)\n", "bunch.show()" ] }, { "cell_type": "markdown", "id": "da158ca0-3b61-4356-be5a-149e791cc2a1", "metadata": {}, "source": [ "### Zooming with clipping\n", "\n", "Removes unseen data points." ] }, { "cell_type": "code", "execution_count": 6, "id": "5a4c0ff9-5cff-4e7c-8726-0aca9c6d4c15", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:32:57.446703Z", "iopub.status.busy": "2024-08-23T10:32:57.446618Z", "iopub.status.idle": "2024-08-23T10:32:57.471103Z", "shell.execute_reply": "2024-08-23T10:32:57.470912Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "p3 = p + ggtitle(\"Default\")\n", "p4 = p + scale_x_continuous(limits=[-92, -82]) + ylim(36, 43) + ggtitle(\"Zoom with ylim()\")\n", "\n", "w, h = 500, 375\n", "bunch = GGBunch()\n", "bunch.add_plot(p3, 0, 0, w, h)\n", "bunch.add_plot(p4, w, 0, w, h)\n", "bunch.show()" ] } ], "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 }