In [1]:
import pandas as pd
from lets_plot import *

def dump_plot(plot, display=None):
    import json

    try:
        import clipboard
    except:
        clipboard = None
        
    from lets_plot._type_utils import standardize_dict
    
    plot_dict = standardize_dict(plot.as_dict())
    plot_json = json.dumps(plot_dict, indent=2)
    
    if clipboard:
        clipboard.copy('')
        clipboard.copy(str(plot_json))
    else:
        if display is None:
            display = True
    
    if display:
        print(plot_json)

    return plot
    
In [2]:
LetsPlot.setup_html()
In [3]:
plot_data = pd.DataFrame.from_records([
    ("pet", "cat", 5, "carnivore"),
    ("pet", "dog", 10, "carnivore"),
    ("pet", "rabbit", 2, "herbivore"),
    ("pet", "hamster", 1, "herbivore"),

    ("farm_animal", "cow", 500, "herbivore"),
    ("farm_animal", "pig", 100, "carnivore"),
    ("farm_animal", "horse", 700, "herbivore"),
])
plot_data.columns = ("animal_type", "animal", "weight", "diet")
In [4]:
plot = (
    ggplot(plot_data, aes(x="animal", y="weight"))
    + geom_bar(stat="identity")
    + theme_bw()
    + theme(
        panel_grid_minor=element_blank()
    )
)
dump_plot(plot)
Out[4]:

Two facets, grid, free scales

In [5]:
plot + facet_grid(x = "animal_type", scales="free")
Out[5]:

Two facets, wrap, free scales

In [6]:
plot + facet_wrap(facets="animal_type", ncol=2, scales="free")
Out[6]:

Four facets, grid, free x

In [7]:
plot + facet_grid(x="animal_type", y="diet", scales="free_x")
Out[7]:

Four facets, wrap, free x

In [8]:
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_x")
Out[8]:

Four facets, grid, free y

In [9]:
plot + facet_grid(x="animal_type", y="diet", scales="free_y")
Out[9]:

Four facets, wrap, free y

In [10]:
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_y")
Out[10]:

Four facets, grid, free scales

In [11]:
plot + facet_grid(x="animal_type", y="diet", scales="free")
Out[11]:

Four facets, wrap, free scales

In [12]:
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free")
Out[12]:

Y-orientation

In [13]:
plot_y = (
    ggplot(plot_data, aes(x="weight", y="animal"))
    + geom_bar(stat="identity", orientation="y")
    + theme_bw()
    + theme(
        panel_grid_minor=element_blank()
    )
)
plot_y
Out[13]:

Two facets, grid, free scales

In [14]:
plot_y + facet_grid(y = "animal_type", scales="free_y")
Out[14]:

Two facets, wrap, free scales

In [15]:
plot_y + facet_wrap(facets="animal_type", ncol=2, scales="free")
Out[15]:

Four facets, grid, free x

In [16]:
plot_y + facet_grid(y="animal_type", x="diet", scales="free_x")
Out[16]:

Four facets, wrap, free x

In [17]:
plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_x")
Out[17]:

Four facets, grid, free y

In [18]:
plot_y + facet_grid(y="animal_type", x="diet", scales="free_y")
Out[18]:

Four facets, wrap, free y

In [19]:
plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_y")
Out[19]:

Four facets, grid, free scales

In [20]:
plot_y + facet_grid(y="animal_type", x="diet", scales="free") + theme(panel_border=element_rect())
Out[20]:

Four facets, wrap, free scales

In [21]:
dump_plot(plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free") + theme(panel_border=element_rect()))
Out[21]:
In [ ]: