#!/usr/bin/env python # coding: utf-8 # # Showing Quantiles on Density Plots # In[1]: import pandas as pd import numpy as np from lets_plot import * # In[2]: LetsPlot.setup_html() # In[3]: df = pd.read_csv("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") print(df.shape) df.head() # #### 1. Parameter `quantile_lines` # In[4]: 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) # In[5]: 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]) # In[6]: 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` # In[7]: quantiles = [0, .02, .3, .7, .98, 1] # In[8]: 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) # In[9]: 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]) # In[10]: 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 # In[11]: 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) # In[12]: 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]) # In[13]: 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])