Download notebook (.ipynb)

Zoom and Pan Interactivity#

Use the ggtb() function to enable Pan and Zoom interactivity on a chart.

This function adds a toolbar containing three tool-buttons: pan, rubber-band zoom, and center-point zoom.

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.

The behavior of these tools adapts to the initial mouse drag direction:

  • Near-horizontal drag: restricts panning to horizontal movement or creates a vertical band for zooming.

  • Near-vertical drag: limits panning to vertical movement or produces a horizontal band for zooming.

  • Diagonal drag: allows panning in any direction or creates a rectangular area for zooming.

Double-clicking anywhere on the plot resets it to its original coordinates, regardless of whether a tool is selected or not.

Click the 4th button, Reset, to reset the plot and tools to their original state.

Parameters:

  • size_zoomin

    The size_zoomin parameter accepts an integer value:

    • 0 — zoom in disabled (default)

    • -1 — unlimited zoom in

    • Any other positive number — maximum zoom-in limit. For example, 2 means the geometry can be scaled up to .

  • size_basis

    The size_basis parameter accepts a string value: x, y, min, or max (default: max). It defines which axis is used to calculate the scaling factor.

    • x and y specify the corresponding axis.

    • min uses the smaller scaling factor.

    • max uses the larger scaling factor.

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(df.shape)
df.head()
(234, 12)
Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy fl class
0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compact
1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compact
2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 p compact
3 4 audi a4 2.0 2008 4 auto(av) f 21 30 p compact
4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compact
import numpy as np
from PIL import Image
import requests
from io import BytesIO

response = requests.get("https://github.com/JetBrains/lets-plot/raw/master/docs/f-24g/images/shevy_impala_64.png")

image = Image.open(BytesIO(response.content))
img = np.asarray(image)
img.shape
(1334, 2000, 4)
p = (ggplot(df)
     + ggtb(size_zoomin=3)           # <--- NEW!
     + theme_bw() 
     + flavor_high_contrast_dark() 
     + theme(legend_position = "none", axis_title="blank")
     + ggsize(1000, 600))

(p + aes('displ', 'hwy', color='manufacturer')
 + geom_imshow(img, extent=[5.5, 7, 35, 45])
 + geom_point(position=position_jitter(height=0, width=0.1, seed=0), 
              tooltips=layer_tooltips(['displ','cyl','trans'])
                      .format("@year", "d")
                      .title('@manufacturer @model @year'))
 + geom_label(aes(label='model'), 
            check_overlap=True, 
            alpha=0.5,
            position=position_jitter(seed=0))
 + ggtitle("Highway MPG vs Engine displacement [L] ")
 + scale_color_brewer(palette="Set3") 
 + scale_continuous(aesthetic=['x', 'y'], position="both")
 + coord_cartesian()
 )