{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Annotation Labels on Pie-Chart\n", " Annotation Labels on Pie-Chart specifies content of the annotations.\n", " \n", " 1. [Default presentation](#1.Default-presentation)\n", " \n", " 2. [Change default annotation](#2.-Change-default-annotation)\n", " \n", " 3. [Use colors of the flavor scheme](#3.-Use-colors-of-the-flavor-scheme)\n", " \n", " 4. [Annotations for Pie-Chart with explode parameter](#4.-Annotations-for-Pie-Chart-with-explode-parameter)\n", " \n", " 5. [Annotations on scatter pie plot](#5.-Annotations-on-scatter-pie-plot)\n", " \n", " 6. [Annotations for Pie-Chart with different sector's size](#6.-Annotations-for-Pie-Chart-with-different-sector's-size) \n", " " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:09.503554Z", "iopub.status.busy": "2024-04-26T11:40:09.503554Z", "iopub.status.idle": "2024-04-26T11:40:10.635269Z", "shell.execute_reply": "2024-04-26T11:40:10.635269Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lets_plot import *\n", "from lets_plot.mapping import *\n", "\n", "LetsPlot.setup_html() " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:10.650897Z", "iopub.status.busy": "2024-04-26T11:40:10.650897Z", "iopub.status.idle": "2024-04-26T11:40:10.682229Z", "shell.execute_reply": "2024-04-26T11:40:10.682229Z" } }, "outputs": [], "source": [ "blank_theme = theme(line=element_blank(), axis_text=element_blank(), axis_title=element_blank(),\n", " legend_position='none')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:10.682229Z", "iopub.status.busy": "2024-04-26T11:40:10.682229Z", "iopub.status.idle": "2024-04-26T11:40:10.839479Z", "shell.execute_reply": "2024-04-26T11:40:10.839479Z" } }, "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", " \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
45audia42.819996auto(l5)f1626pcompact
\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", "4 5 audi a4 2.8 1999 6 auto(l5) f 16 26 \n", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 p compact \n", "3 p compact \n", "4 p compact " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "mpg_df = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")\n", "mpg_df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.Default presentation\n", "\n", "By default, `geom_pie()` doesn't display any annotations and is not very informative." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:10.839479Z", "iopub.status.busy": "2024-04-26T11:40:10.839479Z", "iopub.status.idle": "2024-04-26T11:40:10.965417Z", "shell.execute_reply": "2024-04-26T11:40:10.965417Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + geom_pie(aes(fill='class'), size=22, hole=0.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Change default annotation\n", "\n", "Add simple annotations. \n", "Аnnotations are placed inside sectors. If they do not fit, then external labels will appear." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:10.965417Z", "iopub.status.busy": "2024-04-26T11:40:10.965417Z", "iopub.status.idle": "2024-04-26T11:40:10.996670Z", "shell.execute_reply": "2024-04-26T11:40:10.996670Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + \\\n", " geom_pie(aes(fill='class'), size=22, hole=0.2,\n", " labels=layer_labels().line('@class'),\n", " tooltips='none') + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " blank_theme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add multiline annotations." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:10.996670Z", "iopub.status.busy": "2024-04-26T11:40:10.996670Z", "iopub.status.idle": "2024-04-26T11:40:11.029093Z", "shell.execute_reply": "2024-04-26T11:40:11.029093Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + \\\n", " geom_pie(aes(fill='class'), size=22, hole=0.2,\n", " labels=layer_labels()\n", " .line('@class')\n", " .line('(@{..prop..})')\n", " .format('..prop..', '.0%'),\n", " tooltips='none') + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " blank_theme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can change labels size with `size()` function." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.029093Z", "iopub.status.busy": "2024-04-26T11:40:11.029093Z", "iopub.status.idle": "2024-04-26T11:40:11.060784Z", "shell.execute_reply": "2024-04-26T11:40:11.060784Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + \\\n", " geom_pie(aes(fill=as_discrete('class', order_by='..count..')), size=20, hole=0.2,\n", " labels=layer_labels()\n", " .line('@..proppct.. %')\n", " .format('..proppct..', '.1f')\n", " .size(18),\n", " tooltips='none') + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " blank_theme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more complex settings, you can use theme text settings like `size`, `color`, `bold`, `italic`. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.060784Z", "iopub.status.busy": "2024-04-26T11:40:11.060784Z", "iopub.status.idle": "2024-04-26T11:40:11.092246Z", "shell.execute_reply": "2024-04-26T11:40:11.092246Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + \\\n", " geom_pie(aes(fill=as_discrete('class', order_by='..count..', order=1)), size=20, hole=0.3,\n", " labels=layer_labels()\n", " .line('@..proppct.. %')\n", " .format('..proppct..', '.1f'),\n", " tooltips='none') + \\\n", " blank_theme + \\\n", " theme(text=element_text(face='bold italic', size=16, color='#542788'), legend_position='right') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Use colors of the flavor scheme\n", "\n", "You can combine Pie-Chart with different flavor schemes." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.092246Z", "iopub.status.busy": "2024-04-26T11:40:11.092246Z", "iopub.status.idle": "2024-04-26T11:40:11.123838Z", "shell.execute_reply": "2024-04-26T11:40:11.123838Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + geom_pie(aes(fill=as_discrete('class', order_by='..count..')), size=20, hole=0.2,\n", " labels=layer_labels()\n", " .line('@..proppct.. %')\n", " .format('..proppct..', '.1f')\n", " .size(18),\n", " tooltips='none') + \\\n", " flavor_darcula() + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " blank_theme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Annotations for Pie-Chart with explode parameter" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.123838Z", "iopub.status.busy": "2024-04-26T11:40:11.123838Z", "iopub.status.idle": "2024-04-26T11:40:11.139394Z", "shell.execute_reply": "2024-04-26T11:40:11.139394Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "length = {\n", " 'name' : ['20-50 km', '50-75 km', '10-20 km', '75-100 km', '3-5 km', '7-10 km', '5-7 km', '>100 km', '2-3 km'],\n", " 'count': [1109, 696, 353, 192, 168, 86, 74, 65, 53],\n", " 'explode': [0, 0, 0, 0.1, 0.1, 0.2, 0.3, 0.4, 0.6]\n", "}\n", "\n", "ggplot(length) + \\\n", " geom_pie(aes(fill='name', slice='count', explode='explode'), stat='identity',\n", " stroke=1, color='black', size=20, hole=0.3,\n", " labels=layer_labels(['name', 'count'])) + \\\n", " scale_fill_gradient(low='dark_blue', high='light_green') + \\\n", " blank_theme" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Annotations on scatter pie plot" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.139394Z", "iopub.status.busy": "2024-04-26T11:40:11.139394Z", "iopub.status.idle": "2024-04-26T11:40:11.155114Z", "shell.execute_reply": "2024-04-26T11:40:11.155114Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot(\n", " {\n", " 'x': [1, 1, 1, 1, 1, 1.5, 1.5, 2, 2, 2 ],\n", " 'y': [1, 1, 1, 1, 1, 2, 2, 1.5, 1.5, 1.5],\n", " 's': [3, 1, 2, 1, 4, 1, 3, 3, 2, 1]\n", " }\n", ") + xlim(0.5,2.5) + ylim(0.5,2.5) \\\n", " + geom_pie(aes('x', 'y', slice='s', fill=as_discrete('s')),\n", " size=10, stat='identity',\n", " labels=layer_labels(['s']).size(16),\n", " tooltips='none'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Annotations for Pie-Chart with different sector's size" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:40:11.155114Z", "iopub.status.busy": "2024-04-26T11:40:11.155114Z", "iopub.status.idle": "2024-04-26T11:40:11.170682Z", "shell.execute_reply": "2024-04-26T11:40:11.170682Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot({'n': [\"a\", \"b\", \"c\"], 's': [1, 2, 3]}) + \\\n", " geom_pie(aes(fill='n', slice='s', size='n'), stat=\"identity\",\n", " labels=layer_labels().line('^fill (^slice)')) + \\\n", " blank_theme + \\\n", " theme(legend_position='right')" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 4 }