Download notebook (.ipynb)

Showing Quantiles on Density Plots#

import pandas as pd
import numpy as np

from lets_plot import *
LetsPlot.setup_html()
df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv")
print(df.shape)
df.head()
(234, 12)
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

1. Parameter quantile_lines#

common_args = {'color': "black", 'alpha': .75}
p = ggplot(df, aes("hwy", fill="drv"))
p1 = p + geom_density(**common_args) + \
    ggtitle("geom_density(): quantile_lines=False (default)")
p2 = p + geom_density(quantile_lines=True, **common_args) + \
    ggtitle("geom_density(): quantile_lines=True")
gggrid([p1, p2], ncol=1) + ggsize(700, 500)
p = ggplot(df, aes("hwy", "drv", fill="drv"))
p1 = p + geom_area_ridges() + \
    ggtitle("geom_area_ridges(): quantile_lines=False (default)")
p2 = p + geom_area_ridges(quantile_lines=True) + \
    ggtitle("geom_area_ridges(): quantile_lines=True")
gggrid([p1, p2])
p = ggplot(df, aes("drv", "hwy", fill="drv"))
p1 = p + geom_violin() + \
    ggtitle("geom_violin(): quantile_lines=False (default)")
p2 = p + geom_violin(quantile_lines=True) + \
    ggtitle("geom_violin(): quantile_lines=True")
gggrid([p1, p2])

2. Parameter quantiles#

quantiles = [0, .02, .3, .7, .98, 1]
common_args = {'color': "black", 'alpha': .75, 'quantile_lines': True}
p = ggplot(df, aes("hwy", fill="drv"))
p1 = p + geom_density(**common_args) + \
    ggtitle("geom_density(): quantiles=[.25, .5, .75] (default)")
p2 = p + geom_density(quantiles=quantiles, **common_args) + \
    ggtitle("geom_density(): quantiles={0}".format(quantiles))
gggrid([p1, p2], ncol=1) + ggsize(700, 500)
p = ggplot(df, aes("hwy", "drv", fill="drv"))
p1 = p + geom_area_ridges(quantile_lines=True) + \
    ggtitle("geom_area_ridges(): quantiles=[.25, .5, .75] (default)")
p2 = p + geom_area_ridges(quantile_lines=True, quantiles=quantiles) + \
    ggtitle("geom_area_ridges(): quantiles={0}".format(quantiles))
gggrid([p1, p2])
p = ggplot(df, aes("drv", "hwy", fill="drv"))
p1 = p + geom_violin(quantile_lines=True) + \
    ggtitle("geom_violin(): quantiles=[.25, .5, .75] (default)")
p2 = p + geom_violin(quantile_lines=True, quantiles=quantiles) + \
    ggtitle("geom_violin(): quantiles={0}".format(quantiles))
gggrid([p1, p2])

3. Using ..quantile.. in Aesthetics Mapping#

common_args = {'color': "black", 'alpha': .95}
quantiles = np.linspace(0, 1, 15)
fill_diverging = scale_fill_gradient2(low="white", mid="#3F8F77", high="white", midpoint=0.5)

p = ggplot(df, aes("hwy", group="drv"))
p1 = p + geom_density(**common_args) + \
    ggtitle("geom_density(): fill=None in aes (default)")
p2 = p + geom_density(aes(fill='..quantile..'), quantiles=quantiles, **common_args) + fill_diverging + \
    ggtitle("geom_density(): fill='..quantile..' in aes")
gggrid([p1, p2], ncol=1) + ggsize(700, 500)
p = ggplot(df, aes("hwy", "drv"))
p1 = p + geom_area_ridges() + \
    ggtitle("geom_area_ridges(): fill=None in aes (default)")
p2 = p + geom_area_ridges(aes(fill='..quantile..')) + \
    ggtitle("geom_area_ridges(): fill='..quantile..' in aes")
gggrid([p1, p2])
p = ggplot(df, aes("drv", "hwy"))
p1 = p + geom_violin() + \
    ggtitle("geom_violin(): fill=None in aes (default)")
p2 = p + geom_violin(aes(fill='..quantile..')) + \
    ggtitle("geom_violin(): fill='..quantile..' in aes")
gggrid([p1, p2])