{ "cells": [ { "cell_type": "markdown", "id": "protecting-indication", "metadata": {}, "source": [ "# Pie Chart Stroke and Spacers\n", "\n", "The `stroke` and the `color` aesthetics respectively set **line width** and **line color** of the pie sector arcs. \n", "\n", "The `stroke_side` parameter - \"inner\", \"outer\"(def), \"both\" - specifies where to show the arc.\n", "\n", "By default `stroke` is 0, thus no arc is shown regardless of the value of `stroke_side` parameter.\n", "\n", "Parameters `spacer_width` and `spacer_color` define lines between sectors. The default is a narrow segment of the same color as the plot background. Spacers are not applied to exploded sectors and to the sides of adjacent sectors." ] }, { "cell_type": "code", "execution_count": 1, "id": "basic-maintenance", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The geodata is provided by © OpenStreetMap contributors and is made available here under the Open Database License (ODbL).\n" ] } ], "source": [ "import pandas as pd\n", "\n", "from lets_plot.geo_data import *\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "respective-bridal", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "executed-major", "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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\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", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 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(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "placed-chemical", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + theme_void() + geom_pie(aes(fill='class'), size=20) " ] }, { "cell_type": "markdown", "id": "personal-merchandise", "metadata": {}, "source": [ "#### 1. `stroke` and `color`" ] }, { "cell_type": "code", "execution_count": 5, "id": "77fe3d7c", "metadata": {}, "outputs": [], "source": [ "palettes = scale_fill_brewer(palette='Pastel2') + \\\n", " scale_color_brewer(palette='Set2')" ] }, { "cell_type": "code", "execution_count": 6, "id": "naked-girlfriend", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + theme_void() + \\\n", " geom_pie(aes(fill='class', color='class'), size=20, stroke=7) + \\\n", " palettes" ] }, { "cell_type": "markdown", "id": "rubber-starter", "metadata": {}, "source": [ "#### 2. `stroke_side`" ] }, { "cell_type": "markdown", "id": "advanced-cardiff", "metadata": {}, "source": [ "Note: `stroke=7` is added to parameters in order to make arks visible." ] }, { "cell_type": "code", "execution_count": 7, "id": "surgical-province", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg_df, aes(fill='class', color='class')) + theme_void() + palettes\n", "\n", "gggrid([\n", " p + geom_pie(hole=0.3, stroke=7) + ggtitle(\"Outer stroke (Default)\"),\n", " p + geom_pie(hole=0.3, stroke=7, stroke_side=\"Inner\") + ggtitle(\"Inner stroke\"),\n", " p + geom_pie(hole=0.3, stroke=7, stroke_side=\"both\") + ggtitle(\"Inner & outer stroke\")\n", "]) + ggsize(1000, 200)\n", "\n" ] }, { "cell_type": "markdown", "id": "primary-generator", "metadata": {}, "source": [ "#### 3. `spacer_width` and `spacer_color`" ] }, { "cell_type": "markdown", "id": "broad-policy", "metadata": {}, "source": [ "\"Spacer\" is a thin line separating the pie' slices.\\\n", "You can adjust width and color of spacers." ] }, { "cell_type": "code", "execution_count": 8, "id": "shaped-nicholas", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + theme_void() + \\\n", " geom_pie(aes(fill='class'), \n", " size=20, hole=0.3,\n", " spacer_width=4, spacer_color='light-gray')" ] }, { "cell_type": "markdown", "id": "competent-secretariat", "metadata": {}, "source": [ "##### 3.1 Spacers with Exploded Sectors \n", "\n", "Spacers are not shown for exploded sectors." ] }, { "cell_type": "code", "execution_count": 9, "id": "agricultural-thought", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mpg_df['explode'] = [0.2 if c == 'pickup' else 0.0 for c in mpg_df['class']]\n", "\n", "ggplot(mpg_df) + theme_void() + \\\n", " geom_pie(aes(fill='class', explode='explode'), \n", " size=20, hole=0.3,\n", " stroke=2, color='black',\n", " stroke_side='both',\n", " spacer_width=4, spacer_color='light-gray')" ] }, { "cell_type": "markdown", "id": "offensive-berkeley", "metadata": {}, "source": [ "#### 3. Pie Chart on Map" ] }, { "cell_type": "code", "execution_count": 10, "id": "twelve-worship", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = {\n", " \"city\": [\"New York\", \"New York\", \"Philadelphia\", \"Philadelphia\"],\n", " \"est_pop_2020\": [4_381_593, 3_997_959, 832_685, 748_846],\n", " \"sex\": [\"female\", \"male\", \"female\", \"male\"]\n", "}\n", "\n", "centroids = geocode_cities(data[\"city\"]).get_centroids()\n", "\n", "ggplot() + geom_livemap() + \\\n", " geom_pie(aes(slice='est_pop_2020', fill='sex', size='est_pop_2020'),\n", " stat='identity', data=data, map=centroids, map_join='city', \n", " hole=0.2, alpha=0.6,\n", " color='black', stroke=2, stroke_side='both',\n", " spacer_color='black', spacer_width=2) + \\\n", " scale_size(range=[5,10], guide='none') + labs(fill=\"Gender\")" ] } ], "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.17" } }, "nbformat": 4, "nbformat_minor": 5 }