{ "cells": [ { "cell_type": "markdown", "id": "b0b00c5e-dcef-4bba-a7b8-92ca3320063b", "metadata": {}, "source": [ "# `Zoom` and `Pan` Interactivity\n", "\n", "Use the `ggtb()` function to enable `Pan` and `Zoom` interactivity on a chart.\n", "\n", "This function adds a toolbar containing three tool-buttons: pan, rubber-band zoom, and center-point zoom. \n", "\n", "Each tool uses **mouse-drag** for its specific functionality. Additionally, the **mouse wheel** can be used for zooming in and out, regardless of the selected tool.\n", "\n", "The behavior of these tools adapts to the initial mouse drag direction:\n", "- Near-horizontal drag: restricts panning to horizontal movement or creates a vertical band for zooming.\n", "- Near-vertical drag: limits panning to vertical movement or produces a horizontal band for zooming.\n", "- Diagonal drag: allows panning in any direction or creates a rectangular area for zooming.\n", "\n", "**Double-clicking** anywhere on the plot resets it to its original coordinates, regardless of whether a tool is selected or not.\n", "\n", "Click the 4th button, `Reset`, to reset the plot and tools to their original state.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "c805f48f-bd4a-47ce-b687-db299e29132d", "metadata": { "execution": { "iopub.execute_input": "2025-07-17T17:06:06.660940Z", "iopub.status.busy": "2025-07-17T17:06:06.660741Z", "iopub.status.idle": "2025-07-17T17:06:06.663569Z", "shell.execute_reply": "2025-07-17T17:06:06.663383Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "b846c5b9-5f3d-46a6-95f2-849c6ffd18cf", "metadata": { "execution": { "iopub.execute_input": "2025-07-17T17:06:06.664505Z", "iopub.status.busy": "2025-07-17T17:06:06.664392Z", "iopub.status.idle": "2025-07-17T17:06:06.666177Z", "shell.execute_reply": "2025-07-17T17:06:06.666014Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "113c62dd-177c-4090-b305-6ac953e34dd8", "metadata": { "execution": { "iopub.execute_input": "2025-07-17T17:06:06.666978Z", "iopub.status.busy": "2025-07-17T17:06:06.666908Z", "iopub.status.idle": "2025-07-17T17:06:07.239480Z", "shell.execute_reply": "2025-07-17T17:06:07.239286Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(234, 12)\n" ] }, { "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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "e27c2a9f-f578-4dad-88e0-950d26fba429", "metadata": { "execution": { "iopub.execute_input": "2025-07-17T17:06:07.240589Z", "iopub.status.busy": "2025-07-17T17:06:07.240501Z", "iopub.status.idle": "2025-07-17T17:06:09.637700Z", "shell.execute_reply": "2025-07-17T17:06:09.637380Z" } }, "outputs": [ { "data": { "text/plain": [ "(1334, 2000, 4)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "from PIL import Image\n", "import requests\n", "from io import BytesIO\n", "\n", "response = requests.get(\"https://github.com/JetBrains/lets-plot/raw/master/docs/f-24g/images/shevy_impala_64.png\")\n", "\n", "image = Image.open(BytesIO(response.content))\n", "img = np.asarray(image)\n", "img.shape" ] }, { "cell_type": "code", "execution_count": 5, "id": "3c7b75c3-b6c5-4cd1-88cc-54bde1d94a8c", "metadata": { "execution": { "iopub.execute_input": "2025-07-17T17:06:09.639374Z", "iopub.status.busy": "2025-07-17T17:06:09.639127Z", "iopub.status.idle": "2025-07-17T17:06:10.526878Z", "shell.execute_reply": "2025-07-17T17:06:10.526578Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = (ggplot(df)\n", " + ggtb() # <--- NEW!\n", " + theme_bw() \n", " + flavor_high_contrast_dark() \n", " + theme(legend_position = \"none\", axis_title=\"blank\")\n", " + ggsize(1000, 600))\n", "\n", "(p + aes('displ', 'hwy', color='manufacturer')\n", " + geom_imshow(img, extent=[5.5, 7, 35, 45])\n", " + geom_point(position=position_jitter(height=0, width=0.1, seed=0), \n", " tooltips=layer_tooltips(['displ','cyl','trans'])\n", " .format(\"@year\", \"d\")\n", " .title('@manufacturer @model @year'))\n", " + geom_label(aes(label='model'), \n", " check_overlap=True, \n", " alpha=0.5,\n", " position=position_jitter(seed=0))\n", " + ggtitle(\"Highway MPG vs Engine displacement [L] \")\n", " + scale_color_brewer(palette=\"Set3\") \n", " + scale_continuous(aesthetic=['x', 'y'], position=\"both\")\n", " + coord_cartesian()\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 }