{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "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": {}, "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": {}, "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": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "p = ggplot(mpg_df) + blank_theme + scale_fill_brewer(palette='Dark2')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + 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')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + geom_pie(aes(fill=as_discrete('class', order_by='..count..')), size=20, hole=0.2,\n", " labels=layer_labels()\n", " .line('@class')\n", " .line('(@{..prop..})')\n", " .format('@{..prop..}', '.0%'),\n", " tooltips='none')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Change default annotation's size\n", "\n", "p + 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')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# theme(text) is applying to the annotation's text\n", "\n", "ggplot(mpg_df) + blank_theme + \\\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", " theme(text=element_text(face='bold italic', size=16, color='#542788'), legend_position='right')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Use colors of the flavor scheme\n", "\n", "p + 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() " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "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, stroke_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": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(length) + \\\n", " geom_pie(aes(fill=as_discrete('name', order_by='..count..', order=1), weight='count'), \n", " stroke=1, stroke_color='black', size=22, \n", " labels=layer_labels(['name', '..count..'])) +\\\n", " scale_fill_gradient(low='light_green', high='dark_blue') +\\\n", " blank_theme" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "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": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot({'n': [\"a\", \"b\", \"c\"], 's': [1, 2, 3]}) + blank_theme + \\\n", " geom_pie(aes(fill='n', slice='s', size='n'), stat=\"identity\",\n", " labels=layer_labels().line('^fill (^slice)')) + \\\n", " theme(legend_position='right')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }