{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.202185Z", "iopub.status.busy": "2024-04-17T07:30:32.202097Z", "iopub.status.idle": "2024-04-17T07:30:32.516783Z", "shell.execute_reply": "2024-04-17T07:30:32.516448Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.518460Z", "iopub.status.busy": "2024-04-17T07:30:32.518324Z", "iopub.status.idle": "2024-04-17T07:30:32.520563Z", "shell.execute_reply": "2024-04-17T07:30:32.520376Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.533751Z", "iopub.status.busy": "2024-04-17T07:30:32.533581Z", "iopub.status.idle": "2024-04-17T07:30:32.670782Z", "shell.execute_reply": "2024-04-17T07:30:32.670375Z" } }, "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": [ "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": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.672061Z", "iopub.status.busy": "2024-04-17T07:30:32.671957Z", "iopub.status.idle": "2024-04-17T07:30:32.706614Z", "shell.execute_reply": "2024-04-17T07:30:32.706283Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tooltip_content = (layer_tooltips()\n", " .title('@{class}')\n", " .line('count|@{..count..} (@{..prop..})')\n", " .line('total|@{..sum..}')\n", " .format('..count..', 'd')\n", " .format('..sum..', 'd')\n", " .format('..prop..', '.0%'))\n", "\n", "ggplot(mpg_df) + \\\n", " geom_bar(aes(as_discrete('manufacturer', order=1), fill='class'), tooltips=tooltip_content) + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " ggsize(800, 450)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.707913Z", "iopub.status.busy": "2024-04-17T07:30:32.707811Z", "iopub.status.idle": "2024-04-17T07:30:32.714654Z", "shell.execute_reply": "2024-04-17T07:30:32.714477Z" } }, "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 = '..sum..'),\n", " tooltips=tooltip_content) + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " facet_wrap(facets='manufacturer', ncol=5) + \\\n", " scale_size(range=[4, 10]) + \\\n", " guides(size='none') + \\\n", " theme_void() + theme(strip_background='blank')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:30:32.715894Z", "iopub.status.busy": "2024-04-17T07:30:32.715796Z", "iopub.status.idle": "2024-04-17T07:30:32.720557Z", "shell.execute_reply": "2024-04-17T07:30:32.720374Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(mpg_df) + \\\n", " geom_pie(aes(fill=as_discrete('class', order_by='..count..'), size = '..sum..'), \n", " size=24, hole=0.2, stroke=1.0,\n", " tooltips=layer_tooltips()\n", " .title('@{class}')\n", " .line('Count|@{..count..}')\n", " .line('Percent|@{..proppct..}')\n", " .line('Total|@{..sum..}')\n", " .format('..count..', 'd')\n", " .format('..proppct..', 'd')) + \\\n", " scale_fill_brewer(palette='Dark2') + \\\n", " theme_void()" ] } ], "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 }