Download notebook (.ipynb)

ggbunch()#

ggbunch() allows to show a collection of plots on one figure. Each plot in the collection can have arbitrary location and size. There is no automatic layout inside the bunch.

import numpy as np

from lets_plot import *
LetsPlot.setup_html()
np.random.seed(42)
cov=[[1, 0],
     [0, 1]]
x, y = np.random.multivariate_normal(mean=[0,0], cov=cov, size=400).T

data = dict(
    x = x,
    y = y
)

View this data as a scatter plot and as a histogram#

p = ggplot(data) + ggsize(600, 200)

scatter = p + geom_point(aes('x', 'y'), color='black', alpha=.4)
scatter
histogram = p + geom_histogram(aes('x', y = '..count..'), fill='dark_magenta')
histogram

Combine both plots in one figure#

# Set scale X limits manually because of computed automatically
# the scale used by each plot would be slightly different
# and the stacked plots wouldn't be aligned.
scale_x = scale_x_continuous(limits=[-3.5, 3.5])
ggbunch(
    [
        histogram + scale_x,
        scatter + scale_x
    ],
    [
        (0, 0, 1, .5),
        (0, .5, 1, .5),
    ]
)

Adjust visuals of the bunch figure#

upper_theme = theme(axis_title_x='blank', axis_ticks_x='blank', axis_line='blank', \
                    panel_grid='blank')
lower_theme = theme(axis_text_x='blank', axis_ticks_x='blank', axis_line='blank')
ggbunch(
    [
        histogram + upper_theme + scale_x,
        scatter + lower_theme + scale_x
    ],
    [
        (0, 0, 1, .5),
        (0, .5, 1, .5),
    ]
)

Move the regions#

You can additionally move the bottom plot up a bit with the last two parameters of the tuple responsible for the layout of the region.

ggbunch(
    [
        histogram + upper_theme + scale_x,
        scatter + lower_theme + scale_x
    ],
    [
        (0, 0, 1, .5),
        (0, .5, 1, .5, 0, -12), # Move up 12 pixels
    ]
)

Adjust plot sizes#

The width and height of the plots within ggbunch() are given in relative units, but you can change the size of the whole ggbunch() plot with ggsize().

ggbunch(
    [
        histogram + upper_theme + scale_x,
        scatter + lower_theme + scale_x
    ],
    [
        (0, 0, 1, .5),
        (0, .5, 1, .5, 0, -12),
    ]
) + ggsize(600, 400)