{ "cells": [ { "cell_type": "markdown", "id": "employed-rebate", "metadata": {}, "source": [ "# Annotated Barchart\n", "\n", "Use the `labels` parameter to create an annotated Barchart.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "arranged-meter", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "remarkable-toolbox", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "faced-integral", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
34audia42.020084auto(av)f2130pcompact
\n", "
" ], "text/plain": [ " Unnamed: 0 manufacturer model displ year cyl trans drv cty hwy \\\n", "0 1 audi a4 1.8 1999 4 auto(l5) f 18 29 \n", "1 2 audi a4 1.8 1999 4 manual(m5) f 21 29 \n", "2 3 audi a4 2.0 2008 4 manual(m6) f 20 31 \n", "3 4 audi a4 2.0 2008 4 auto(av) f 21 30 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg_df.head(4)" ] }, { "cell_type": "markdown", "id": "72ed6d0b-27f3-4f7a-82a1-7b93205a1092", "metadata": {}, "source": [ "#### Configure Layer Labels Using API Similar to the Tooltip Configuration API" ] }, { "cell_type": "code", "execution_count": 4, "id": "8083daa6-66c4-47d1-84ee-014ed78df766", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " ggplot(mpg_df) + geom_bar(aes(\"trans\"),\n", " labels=layer_labels().line('@..count..')),\n", " ggplot(mpg_df) + geom_bar(aes(\"trans\", y=\"..n..\"), stat=\"sum\", size=0,\n", " labels=layer_labels().line('@..proppct..')) + coord_flip()\n", "], widths=[1, 1.2]) \n" ] }, { "cell_type": "markdown", "id": "economic-overview", "metadata": {}, "source": [ "#### Stacked Bars" ] }, { "cell_type": "code", "execution_count": 5, "id": "temporal-alias", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df, aes('class', fill='drv')) + \\\n", " geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %')) + \\\n", " ggsize(700, 400)" ] }, { "cell_type": "code", "execution_count": 6, "id": "ambient-science", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df, aes('class', fill='drv')) + \\\n", " geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'), \n", " position='fill') + \\\n", " coord_flip() + \\\n", " ggsize(700, 400)" ] }, { "cell_type": "markdown", "id": "vertical-phrase", "metadata": {}, "source": [ "#### Narrow Bars\n", "Too thin for labels to fit in. " ] }, { "cell_type": "code", "execution_count": 7, "id": "excited-darwin", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df, aes('class', fill='drv')) + \\\n", " geom_bar(labels=layer_labels().line('@..proppct..').format('..proppct..', '{d} %'),\n", " position='dodge') + \\\n", " ggsize(700, 400)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.18" } }, "nbformat": 4, "nbformat_minor": 5 }