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
LetsPlot.setup_html()
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")
plot = (
ggplot(plot_data, aes(x="animal", y="weight"))
+ geom_bar(stat="identity")
+ theme_bw()
+ theme(
panel_grid_minor=element_blank()
)
)
dump_plot(plot)
plot + facet_grid(x = "animal_type", scales="free")
plot + facet_wrap(facets="animal_type", ncol=2, scales="free")
plot + facet_grid(x="animal_type", y="diet", scales="free_x")
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_x")
plot + facet_grid(x="animal_type", y="diet", scales="free_y")
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_y")
plot + facet_grid(x="animal_type", y="diet", scales="free")
plot + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free")
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
plot_y + facet_grid(y = "animal_type", scales="free_y")
plot_y + facet_wrap(facets="animal_type", ncol=2, scales="free")
plot_y + facet_grid(y="animal_type", x="diet", scales="free_x")
plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_x")
plot_y + facet_grid(y="animal_type", x="diet", scales="free_y")
plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free_y")
plot_y + facet_grid(y="animal_type", x="diet", scales="free") + theme(panel_border=element_rect())
dump_plot(plot_y + facet_wrap(facets=["animal_type", "diet"], ncol=2, scales="free") + theme(panel_border=element_rect()))