#!/usr/bin/env python # coding: utf-8 # # Annotated Barchart # # Use the `labels` parameter to create an annotated Barchart. # # # In[1]: import pandas as pd from lets_plot import * # In[2]: LetsPlot.setup_html() # In[3]: mpg_df = pd.read_csv ("https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv") mpg_df.head(4) # #### Configure Layer Labels Using API Similar to the Tooltip Configuration API # In[4]: gggrid([ ggplot(mpg_df) + geom_bar(aes("trans"), labels=layer_labels().line('@..count..')), ggplot(mpg_df) + geom_bar(aes("trans", y="..n.."), stat="sum", size=0, labels=layer_labels().line('@..proppct..')) + coord_flip() ], widths=[1, 1.2]) # #### Stacked Bars # In[5]: ggplot(mpg_df, aes('class', fill='drv')) + \ geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %')) + \ ggsize(700, 400) # In[6]: ggplot(mpg_df, aes('class', fill='drv')) + \ geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'), position='fill') + \ coord_flip() + \ ggsize(700, 400) # #### Narrow Bars # Too thin for labels to fit in. # In[7]: ggplot(mpg_df, aes('class', fill='drv')) + \ geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'), position='dodge') + \ ggsize(700, 400)