{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.720314Z", "iopub.status.busy": "2025-11-05T13:44:56.720232Z", "iopub.status.idle": "2025-11-05T13:44:56.723542Z", "shell.execute_reply": "2025-11-05T13:44:56.723268Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.724442Z", "iopub.status.busy": "2025-11-05T13:44:56.724367Z", "iopub.status.idle": "2025-11-05T13:44:56.726344Z", "shell.execute_reply": "2025-11-05T13:44:56.726113Z" } }, "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": "2025-11-05T13:44:56.740419Z", "iopub.status.busy": "2025-11-05T13:44:56.740318Z", "iopub.status.idle": "2025-11-05T13:44:56.827639Z", "shell.execute_reply": "2025-11-05T13:44:56.827407Z" } }, "outputs": [], "source": [ "mpg = pd.read_csv (\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.828721Z", "iopub.status.busy": "2025-11-05T13:44:56.828637Z", "iopub.status.idle": "2025-11-05T13:44:56.830759Z", "shell.execute_reply": "2025-11-05T13:44:56.830551Z" } }, "outputs": [], "source": [ "p = ggplot(mpg) + theme(axis_title_x='blank')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.831658Z", "iopub.status.busy": "2025-11-05T13:44:56.831581Z", "iopub.status.idle": "2025-11-05T13:44:56.885844Z", "shell.execute_reply": "2025-11-05T13:44:56.885604Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = p + ggtitle(\"default\") + geom_boxplot( aes('class','hwy'))\n", "\n", "p2 = (p + ggtitle(\"alphabetically\") \n", " + geom_boxplot(aes(as_discrete('class',order=1), 'hwy')))\n", "p3 = (p + ggtitle(\"middle ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..middle..'), 'hwy')))\n", "p4 = (p + ggtitle(\"middle ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..middle..'), 'hwy')))\n", "p5 = (p + ggtitle(\"ymax ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..ymax..'), 'hwy')))\n", "p6 = (p + ggtitle(\"ymax ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..ymax..'), 'hwy')))\n", "p7 = (p + ggtitle(\"ymin ↑\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=1, order_by='..ymin..'), 'hwy')))\n", "p8 = (p + ggtitle(\"ymin ↓\") \\\n", " + geom_boxplot(aes(as_discrete('class',order=-1, order_by='..ymin..'), 'hwy')))\n", "\n", "gggrid([p1, p2, p3, p4, p5, p6, p7, p8], ncol=2) + ggsize(940, 1200)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.886938Z", "iopub.status.busy": "2025-11-05T13:44:56.886861Z", "iopub.status.idle": "2025-11-05T13:44:56.912708Z", "shell.execute_reply": "2025-11-05T13:44:56.912516Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ordered_class = as_discrete('class', order=1)\n", "\n", "p1 = (p + ggtitle(\"x='class',\\ncolor='class'\") \n", " + geom_boxplot(aes('class', 'hwy', color='class'))\n", " )\n", "\n", "p3 = (p + ggtitle(\"x=as_discrete('class', order=1),\\ncolor='class'\") \n", " + geom_boxplot(aes(ordered_class, 'hwy', color='class'))\n", " )\n", "\n", "p4 = (p + ggtitle(\"x=as_discrete('class', order=1),\\ncolor=as_discrete('class')\") \n", " + geom_boxplot(aes(ordered_class, 'hwy', color=as_discrete('class')))\n", " )\n", "\n", "p5 = p + ggtitle(\"x='class',\\ncolor='drv'\") + geom_boxplot(aes('class','hwy', color='drv'))\n", "\n", "p7 = (p + ggtitle(\"x=as_discrete('class', order=1),\\ncolor=as_discrete('drv', order=1)\") \n", " + geom_boxplot(aes(\n", " as_discrete('class', order=1), 'hwy', \n", " color=as_discrete('drv', order=1)))\n", " )\n", "\n", "p8 = (p + ggtitle(\"x=as_discrete('class', order_by='..middle..')\") \n", " + geom_boxplot(aes(\n", " as_discrete('class', order_by='..middle..', order=1), 'hwy', color='drv'))\n", " )\n", "\n", "ggbunch(\n", " [p1, p3, p4, p5, p7, p8],\n", " [(0, 0, .5, .25),\n", " (0, .25, .5, .25),\n", " (.5, .25, .5, .25),\n", " (0, .5, .5, .25),\n", " (0, .75, .5, .25),\n", " (.5, .75, .5, .25)]\n", ") + ggsize(940, 1200)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.913727Z", "iopub.status.busy": "2025-11-05T13:44:56.913652Z", "iopub.status.idle": "2025-11-05T13:44:56.926246Z", "shell.execute_reply": "2025-11-05T13:44:56.926059Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = (ggplot(mpg, aes('class', 'hwy')) + \n", " geom_crossbar(aes(color='class'), stat='boxplot') + \n", " scale_color_brewer(type='qual', palette='Dark2') + \n", " ggtitle('default'))\n", "\n", "p2 = (ggplot(mpg, aes(x=as_discrete('class', order_by=\"..middle..\", order=1), y = 'hwy')) + \n", " geom_crossbar(aes(color='class'), stat='boxplot') + \n", " scale_color_brewer(type='qual', palette='Dark2') +\n", " ggtitle('middle ↑,\\norder option also applies to \\'color\\'') )\n", "\n", "p3 = (ggplot(mpg, aes(x=as_discrete('class', order_by=\"..middle..\"), y = 'hwy')) + \n", " geom_crossbar(aes(color=as_discrete('class', order=1)), stat='boxplot') +\n", " scale_color_brewer(type='qual', palette='Dark2') + \n", " ggtitle('\\'class\\' with \\'order_by\\' + \\'class\\' with \\'order\\':\\noptions are combined') )\n", "\n", "gggrid([p1, p2, p3], ncol=2) + ggsize(940, 600)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.927147Z", "iopub.status.busy": "2025-11-05T13:44:56.927076Z", "iopub.status.idle": "2025-11-05T13:44:56.937823Z", "shell.execute_reply": "2025-11-05T13:44:56.937636Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p1 = (p + ggtitle(\"default\") + geom_bar(aes('class', 'hwy')))\n", "\n", "p2 = (p + ggtitle(\"alphabetically\") \n", " + geom_bar(aes(as_discrete('class', order=1), 'hwy')))\n", "\n", "\n", "p3 = (p + ggtitle(\"y ↓\") \n", " + geom_bar(aes(as_discrete('class', order_by='hwy'), 'hwy')))\n", "p4 = (p + ggtitle(\"y ↑\") \n", " + geom_bar(aes(as_discrete('class', order_by='hwy', order=1), 'hwy')))\n", "\n", "gggrid([p1, p2, p3, p4], ncol=2) + ggsize(940, 600)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.938779Z", "iopub.status.busy": "2025-11-05T13:44:56.938705Z", "iopub.status.idle": "2025-11-05T13:44:56.961667Z", "shell.execute_reply": "2025-11-05T13:44:56.961472Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# geom_bar\n", "\n", "p1 = (p + ggtitle(\"default\") + geom_bar(aes('manufacturer', fill='class')))\n", "\n", "p2 = (p + ggtitle(\"x alphabetically\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill='class')))\n", "\n", "p3 = (p + ggtitle(\"x alphabetically + sampling_pick(4)\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill='class'), \n", " sampling=sampling_pick(4)))\n", "\n", "p4 = (p + ggtitle(\"fill alphabetically\") \n", " + geom_bar(aes('manufacturer', fill=as_discrete('class',order=1))))\n", "\n", "p5 = (p + ggtitle(\"x and fill alphabetically\") \n", " + geom_bar(aes(as_discrete('manufacturer', order=1), fill=as_discrete('class',order=1))))\n", "\n", "p6 = (p + ggtitle(\"x by count (sum is taken)\") \n", " + geom_bar(aes(as_discrete('manufacturer',order_by='..count..'), fill='class')))\n", "\n", "p7 = (p + ggtitle(\"x by count + sampling_pick(4)\") \n", " + geom_bar(aes(as_discrete('manufacturer',order_by='..count..'), fill='class'),\n", " sampling=sampling_pick(4)))\n", "\n", "ggbunch(\n", " [p1, p2, p3, p4, p5, p6, p7],\n", " [(0, 0, .5, .25),\n", " (0, .25, .5, .25),\n", " (.5, .25, .5, .25),\n", " (0, .5, .5, .25),\n", " (.5, .5, .5, .25),\n", " (0, .75, .5, .25),\n", " (.5, .75, .5, .25)]\n", ") + ggsize(940, 1200)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.962739Z", "iopub.status.busy": "2025-11-05T13:44:56.962662Z", "iopub.status.idle": "2025-11-05T13:44:56.981253Z", "shell.execute_reply": "2025-11-05T13:44:56.981067Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pp = ggplot(mpg, aes(x='fl', fill='drv'))\n", "p1 = pp + geom_bar() + ggtitle('Default')\n", "p2 = pp + geom_bar(position='dodge') + ggtitle(\"With Dodge Position\")\n", "\n", "p_order1 = ggplot(mpg, aes(x=as_discrete('fl',order=1), fill=as_discrete('drv',order=1)))\n", "title1 = ggtitle('x and fill alphabetically')\n", "p3 = p_order1 + geom_bar() + title1\n", "p4 = p_order1 + geom_bar(position='dodge') + title1\n", "\n", "p_order2 = ggplot(mpg, aes(x=as_discrete('fl',order_by=\"..count..\"), fill='drv'))\n", "title2 = ggtitle('x by count')\n", "p5 = p_order2 + geom_bar() + title2\n", "p6 = p_order2 + geom_bar(position='dodge') + title2\n", "\n", "gggrid([p1, p2, p3, p4, p5, p6], ncol=2) + ggsize(940, 900)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:56.982284Z", "iopub.status.busy": "2025-11-05T13:44:56.982213Z", "iopub.status.idle": "2025-11-05T13:44:57.519087Z", "shell.execute_reply": "2025-11-05T13:44:57.518721Z" } }, "outputs": [], "source": [ "# diamonds\n", "\n", "diamonds = pd.read_csv('https://vincentarelbundock.github.io/Rdatasets/csv/ggplot2/diamonds.csv')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:44:57.520444Z", "iopub.status.busy": "2025-11-05T13:44:57.520361Z", "iopub.status.idle": "2025-11-05T13:45:00.053325Z", "shell.execute_reply": "2025-11-05T13:45:00.052982Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pd = ggplot(diamonds)\n", "\n", "p1 = pd + geom_bar(aes('cut', fill = 'clarity')) + ggtitle('Default')\n", "p2 = pd + geom_bar(aes('cut', fill = 'clarity'), position = 'fill') + ggtitle('position=\\'fill\\'')\n", "\n", "p3 = pd + ggtitle('x and fill alphabetically') \\\n", " + geom_bar(aes(as_discrete('cut',order=1), fill = as_discrete('clarity',order=1)), position = 'fill') \n", "p4 = pd + ggtitle('x by count') \\\n", " + geom_bar(aes(as_discrete('cut',order_by='..count..'), \n", " fill = as_discrete('clarity',order=1)),\n", " position = 'fill')\n", "\n", "p5 = pd + geom_bar(aes('cut', fill = 'clarity'), position = 'dodge') + ggtitle('position=\\'dodge\\'')\n", "p6 = pd + ggtitle('x by count and fill alphabetically') \\\n", " + geom_bar(aes(as_discrete('cut',order_by='..count..', order=1),\n", " fill = as_discrete('clarity',order=1)),\n", " position = 'dodge')\n", "\n", "gggrid([p1, p2, p3, p4, p5, p6], ncol=2) + ggsize(940, 900)" ] } ], "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 }