{ "cells": [ { "cell_type": "markdown", "id": "19b9803d-6540-4da0-b46a-8591e3aba21e", "metadata": {}, "source": [ "# Interactive Pan/Zoom with Shared Axes in `gggrid` \n", "When subplots in a `gggrid` share axes via `sharex` or `sharey`, interactive pan and zoom propagate across sibling plots. Zooming or panning one subplot automatically updates all plots sharing that axis. \n", " \n", "**Tip:** double-click a subplot to reset it.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "69bad997-f94f-4a5a-8db4-5b2afb9a663c", "metadata": {}, "outputs": [], "source": [ "from lets_plot import *\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "id": "b272a8c1-ec28-431f-b375-1aa64901ba33", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "e0f6f6f6-9b16-4173-91d8-4ac964632959", "metadata": {}, "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", "
miles per gallonnumber of cylindersengine displacement (cu. inches)engine horsepowervehicle weight (lbs.)time to accelerate (sec.)model yearorigin of carvehicle name
018.08307.0130350412.070USchevrolet chevelle malibu
115.08350.0165369311.570USbuick skylark 320
218.08318.0150343611.070USplymouth satellite
316.08304.0150343312.070USamc rebel sst
417.08302.0140344910.570USford torino
\n", "
" ], "text/plain": [ " miles per gallon number of cylinders engine displacement (cu. inches) \\\n", "0 18.0 8 307.0 \n", "1 15.0 8 350.0 \n", "2 18.0 8 318.0 \n", "3 16.0 8 304.0 \n", "4 17.0 8 302.0 \n", "\n", " engine horsepower vehicle weight (lbs.) time to accelerate (sec.) \\\n", "0 130 3504 12.0 \n", "1 165 3693 11.5 \n", "2 150 3436 11.0 \n", "3 150 3433 12.0 \n", "4 140 3449 10.5 \n", "\n", " model year origin of car vehicle name \n", "0 70 US chevrolet chevelle malibu \n", "1 70 US buick skylark 320 \n", "2 70 US plymouth satellite \n", "3 70 US amc rebel sst \n", "4 70 US ford torino " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/mpg2.csv\")\n", "data.head()" ] }, { "cell_type": "code", "execution_count": 4, "id": "8afacf2f-0894-43ec-adc2-3d6239682bae", "metadata": {}, "outputs": [], "source": [ "mpg='miles per gallon'\n", "weight='vehicle weight (lbs.)'\n", "origin='origin of car'\n", "hp='engine horsepower'\n", "accel='time to accelerate (sec.)'\n", "displ='engine displacement (cu. inches)'\n", "origin='origin of car'" ] }, { "cell_type": "markdown", "id": "0ca8fefa-7f94-4948-8c17-85b214af258d", "metadata": {}, "source": [ "#### Shared X-axis (all)" ] }, { "cell_type": "code", "execution_count": 5, "id": "d399ede0-75c6-4dd3-bb9d-7cc94bee3766", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(data, aes(mpg, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs MPG')\n", "p2 = ggplot(data, aes(mpg, hp)) + geom_point(aes(color=origin)) + ggtitle('HP vs MPG')\n", "p3 = ggplot(data, aes(mpg, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs MPG')\n", "p4 = ggplot(data, aes(mpg, displ)) + geom_point(aes(color=origin)) + ggtitle('Displ vs MPG')\n", "\n", "gggrid([p1, p2, p3, p4], ncol=2, guides='collect', \n", " \n", " sharex='all'\n", " \n", " ) + ggsize(800, 600) + ggtb()\n" ] }, { "cell_type": "markdown", "id": "87d308c2-5c92-4f4b-95d9-f41dccb21973", "metadata": {}, "source": [ "#### Shared Y-axis (all)" ] }, { "cell_type": "code", "execution_count": 6, "id": "3f2a482a-be27-4eec-8702-6edfe4a268bc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(data, aes(displ, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Displ')\n", "p2 = ggplot(data, aes(hp, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs HP')\n", "p3 = ggplot(data, aes(weight, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Weight')\n", "p4 = ggplot(data, aes(accel, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Accel')\n", "\n", "gggrid([p1, p2, p3, p4], ncol=2, guides='collect', \n", " \n", " sharey='all'\n", " \n", " ) + ggsize(800, 600) + ggtb()\n" ] }, { "cell_type": "markdown", "id": "28beca48-2e85-416c-841d-d8101c892425", "metadata": {}, "source": [ "#### Shared X by Column, Y by Row" ] }, { "cell_type": "code", "execution_count": 7, "id": "d6d0a4bb-e85c-4ec8-b610-8354db57abd2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(data, aes(mpg, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs MPG')\n", "p2 = ggplot(data, aes(hp, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs HP')\n", "p3 = ggplot(data, aes(mpg, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs MPG')\n", "p4 = ggplot(data, aes(hp, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs HP')\n", "\n", "gggrid([p1, p2, p3, p4], ncol=2, guides='collect', \n", " \n", " sharex='col', \n", " sharey='row'\n", " \n", " ) + ggsize(800, 600) + ggtb()\n" ] }, { "cell_type": "markdown", "id": "cc7bf3b3-7db6-4751-ad35-bb1060dd93ae", "metadata": {}, "source": [ "#### Shared both X and Y (all)" ] }, { "cell_type": "code", "execution_count": 8, "id": "b5539a39-0759-448d-87d2-e018112c3802", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = ggplot(data[data[origin] == 'US'], aes(mpg, hp)) + geom_point(color='red') + ggtitle('US')\n", "p2 = ggplot(data[data[origin] == 'Europe'], aes(mpg, hp)) + geom_point(color='blue') + ggtitle('Europe')\n", "p3 = ggplot(data[data[origin] == 'Asia'], aes(mpg, hp)) + geom_point(color='green') + ggtitle('Asia')\n", "\n", "gggrid([p1, p2, p3], ncol=3, \n", " \n", " sharex='all', \n", " sharey='all'\n", " \n", " ) + ggsize(900, 350) + ggtb()" ] } ], "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.9.23" } }, "nbformat": 4, "nbformat_minor": 5 }