{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# New `Scale` Functions with Parameter `aesthetic`\n", "\n", "- `scale_identity(aesthetic, *, ...)`\n", "- `scale_manual(aesthetic, values, *, ...)`\n", "- `scale_continuous(aesthetic, *, ...)`\n", "- `scale_gradient(aesthetic, *, ...)`\n", "- `scale_gradient2(aesthetic, *, ...)`\n", "- `scale_gradientn(aesthetic, *, ...)`\n", "- `scale_hue(aesthetic, *, ...)`\n", "- `scale_discrete(aesthetic, *, ...)`\n", "- `scale_grey(aesthetic, *, ...)`\n", "- `scale_brewer(aesthetic, *, ...)`\n", "- `scale_viridis(aesthetic, *, ...)`\n", "- `scale_cmapmpl(aesthetic, *, ...)`\n", "\n", "Comparing to familiar \"scale\" functions like `scale_color_gradient()` etc., the new set of functions\n", "adds more flexibility by allowing specifying an aesthetic or a list of aesthetics the scale is working with.\n", "\n", "For example, you can use just one function call to setup the same color palette for both, stroke and fill colors on plot:\n", "\n", "`scale_brewer(['color', 'fill'], palette='Set2')`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:39:23.570926Z", "iopub.status.busy": "2025-11-05T13:39:23.570827Z", "iopub.status.idle": "2025-11-05T13:39:23.573984Z", "shell.execute_reply": "2025-11-05T13:39:23.573780Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:39:23.574825Z", "iopub.status.busy": "2025-11-05T13:39:23.574723Z", "iopub.status.idle": "2025-11-05T13:39:23.576438Z", "shell.execute_reply": "2025-11-05T13:39:23.576269Z" } }, "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:39:23.589646Z", "iopub.status.busy": "2025-11-05T13:39:23.589503Z", "iopub.status.idle": "2025-11-05T13:39:23.677573Z", "shell.execute_reply": "2025-11-05T13:39:23.677092Z" } }, "outputs": [], "source": [ "mpg_df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg.csv\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 1. Plot with Default Colors" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:39:23.678748Z", "iopub.status.busy": "2025-11-05T13:39:23.678627Z", "iopub.status.idle": "2025-11-05T13:39:23.733610Z", "shell.execute_reply": "2025-11-05T13:39:23.733359Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg_df, aes(as_discrete('drv', order=-1), 'hwy')) + \\\n", " geom_violin(aes(color='drv', fill='drv'), alpha=.5, size=2)\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 2. Setup a Brewer Palette" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 2.1. Old School: for Each Aesthetic Separately" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:39:23.734506Z", "iopub.status.busy": "2025-11-05T13:39:23.734425Z", "iopub.status.idle": "2025-11-05T13:39:23.755537Z", "shell.execute_reply": "2025-11-05T13:39:23.755350Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_color_brewer(palette='Set2') + scale_fill_brewer(palette='Set2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### 2.2. New: for Both Aesthetics at Once" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2025-11-05T13:39:23.756595Z", "iopub.status.busy": "2025-11-05T13:39:23.756521Z", "iopub.status.idle": "2025-11-05T13:39:23.777869Z", "shell.execute_reply": "2025-11-05T13:39:23.777683Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_brewer(['color', 'fill'], palette='Set2')" ] } ], "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 }