{ "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", "\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')`\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:06.621569Z", "iopub.status.busy": "2024-04-17T07:27:06.621252Z", "iopub.status.idle": "2024-04-17T07:27:06.938730Z", "shell.execute_reply": "2024-04-17T07:27:06.938441Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *\n", "from lets_plot.mapping import as_discrete" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2024-04-17T07:27:06.940423Z", "iopub.status.busy": "2024-04-17T07:27:06.940193Z", "iopub.status.idle": "2024-04-17T07:27:06.942452Z", "shell.execute_reply": "2024-04-17T07:27:06.942282Z" } }, "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:27:06.955689Z", "iopub.status.busy": "2024-04-17T07:27:06.955579Z", "iopub.status.idle": "2024-04-17T07:27:07.096376Z", "shell.execute_reply": "2024-04-17T07:27:07.096065Z" } }, "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": "2024-04-17T07:27:07.097580Z", "iopub.status.busy": "2024-04-17T07:27:07.097482Z", "iopub.status.idle": "2024-04-17T07:27:07.149211Z", "shell.execute_reply": "2024-04-17T07:27:07.148867Z" } }, "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": "2024-04-17T07:27:07.150873Z", "iopub.status.busy": "2024-04-17T07:27:07.150467Z", "iopub.status.idle": "2024-04-17T07:27:07.171425Z", "shell.execute_reply": "2024-04-17T07:27:07.171104Z" } }, "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": "2024-04-17T07:27:07.172542Z", "iopub.status.busy": "2024-04-17T07:27:07.172437Z", "iopub.status.idle": "2024-04-17T07:27:07.193149Z", "shell.execute_reply": "2024-04-17T07:27:07.192964Z" } }, "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 }