Download notebook (.ipynb)

Interactive Pan/Zoom with Shared Axes in gggrid#

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.

Tip: double-click a subplot to reset it.

from lets_plot import *
import pandas as pd
LetsPlot.setup_html()
data = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/refs/heads/master/data/mpg2.csv")
data.head()
miles per gallon number of cylinders engine displacement (cu. inches) engine horsepower vehicle weight (lbs.) time to accelerate (sec.) model year origin of car vehicle name
0 18.0 8 307.0 130 3504 12.0 70 US chevrolet chevelle malibu
1 15.0 8 350.0 165 3693 11.5 70 US buick skylark 320
2 18.0 8 318.0 150 3436 11.0 70 US plymouth satellite
3 16.0 8 304.0 150 3433 12.0 70 US amc rebel sst
4 17.0 8 302.0 140 3449 10.5 70 US ford torino
mpg='miles per gallon'
weight='vehicle weight (lbs.)'
origin='origin of car'
hp='engine horsepower'
accel='time to accelerate (sec.)'
displ='engine displacement (cu. inches)'
origin='origin of car'

Shared X-axis (all)#

p1 = ggplot(data, aes(mpg, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs MPG')
p2 = ggplot(data, aes(mpg, hp)) + geom_point(aes(color=origin)) + ggtitle('HP vs MPG')
p3 = ggplot(data, aes(mpg, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs MPG')
p4 = ggplot(data, aes(mpg, displ)) + geom_point(aes(color=origin)) + ggtitle('Displ vs MPG')

gggrid([p1, p2, p3, p4], ncol=2, guides='collect', 
       
       sharex='all'
       
      ) + ggsize(800, 600) + ggtb()

Shared Y-axis (all)#

p1 = ggplot(data, aes(displ, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Displ')
p2 = ggplot(data, aes(hp, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs HP')
p3 = ggplot(data, aes(weight, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Weight')
p4 = ggplot(data, aes(accel, mpg)) + geom_point(aes(color=origin)) + ggtitle('MPG vs Accel')

gggrid([p1, p2, p3, p4], ncol=2, guides='collect', 
       
       sharey='all'
       
      ) + ggsize(800, 600) + ggtb()

Shared X by Column, Y by Row#

p1 = ggplot(data, aes(mpg, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs MPG')
p2 = ggplot(data, aes(hp, weight)) + geom_point(aes(color=origin)) + ggtitle('Weight vs HP')
p3 = ggplot(data, aes(mpg, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs MPG')
p4 = ggplot(data, aes(hp, accel)) + geom_point(aes(color=origin)) + ggtitle('Accel vs HP')

gggrid([p1, p2, p3, p4], ncol=2, guides='collect', 
       
       sharex='col', 
       sharey='row'
       
      ) + ggsize(800, 600) + ggtb()

Shared both X and Y (all)#

p1 = ggplot(data[data[origin] == 'US'], aes(mpg, hp)) + geom_point(color='red') + ggtitle('US')
p2 = ggplot(data[data[origin] == 'Europe'], aes(mpg, hp)) + geom_point(color='blue') + ggtitle('Europe')
p3 = ggplot(data[data[origin] == 'Asia'], aes(mpg, hp)) + geom_point(color='green') + ggtitle('Asia')

gggrid([p1, p2, p3], ncol=3, 
       
       sharex='all', 
       sharey='all'
       
      ) + ggsize(900, 350) + ggtb()