import pandas as pd
from lets_plot import *
from lets_plot.mapping import as_discrete
LetsPlot.setup_html()
QUANTILES = [.25, .5, .75]
def plot_matrix(plots=[], width=400, height=300, columns=2):
bunch = GGBunch()
for i in range(len(plots)):
row = int(i / columns)
column = i % columns
bunch.add_plot(plots[i], column * width, row * height, width, height)
return bunch.show()
mpg_df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
mpg_df.head()
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 |
ggplot(mpg_df, aes(y='hwy')) + geom_violin() + ggtitle("Simplest example")
p_d = ggplot(mpg_df) + \
geom_density(aes(x='hwy', fill='drv'), color='black', alpha=.5) + \
facet_grid(x='drv') + \
coord_flip() + \
ggtitle("geom_density()")
p_v = ggplot(mpg_df, aes(x=as_discrete('drv', order=1), y='hwy')) + \
geom_violin(aes(fill='drv'), alpha=.5) + \
ggtitle("geom_violin()")
plot_matrix([p_d, p_v])
quantiles
¶tests = [
{'quantiles': None},
{'quantiles': []},
{'quantiles': [.05, .5, .95]},
{'quantiles': [.25]},
{'quantiles': [0, .5, 1]},
]
plot_matrix([
ggplot(mpg_df, aes('drv', 'hwy')) + \
geom_violin(quantiles=test['quantiles'], quantile_lines=True) + \
ggtitle("quantiles={0}".format(test['quantiles']))
for test in tests
])