{ "cells": [ { "cell_type": "markdown", "id": "answering-manner", "metadata": {}, "source": [ "# Using Dictionaries for Customizing Scale' Breaks, Labels and Values\n", "\n", "`breaks` and `lables` arguments in `scale_xxx()` function family each accepts list of values for customizing the scale breaks or labels.\n", "\n", "However, it is often more convenient to use a dictionary of breaks (in the `labels` parameter) or a dictionary of labels (in the `breaks` parameter) for the breaks/labels customization.\n", "\n", "Similar situation is with parameters `breaks` and `values` in the `scale_xxx_manual()` function family.\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "pressed-angola", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:54.601532Z", "iopub.status.busy": "2024-11-01T20:16:54.601453Z", "iopub.status.idle": "2024-11-01T20:16:54.927606Z", "shell.execute_reply": "2024-11-01T20:16:54.927192Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "recorded-horizon", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:54.929057Z", "iopub.status.busy": "2024-11-01T20:16:54.928909Z", "iopub.status.idle": "2024-11-01T20:16:54.931354Z", "shell.execute_reply": "2024-11-01T20:16:54.931120Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "honest-stewart", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:54.932384Z", "iopub.status.busy": "2024-11-01T20:16:54.932314Z", "iopub.status.idle": "2024-11-01T20:16:55.272980Z", "shell.execute_reply": "2024-11-01T20:16:55.272473Z" } }, "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", "
Unnamed: 0manufacturermodeldisplyearcyltransdrvctyhwyflclass
01audia41.819994auto(l5)f1829pcompact
12audia41.819994manual(m5)f2129pcompact
23audia42.020084manual(m6)f2031pcompact
\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", "\n", " fl class \n", "0 p compact \n", "1 p compact \n", "2 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(3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "previous-serbia", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:55.274141Z", "iopub.status.busy": "2024-11-01T20:16:55.274050Z", "iopub.status.idle": "2024-11-01T20:16:55.304556Z", "shell.execute_reply": "2024-11-01T20:16:55.304339Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = ggplot(mpg_df, aes(x='displ', y='hwy', color='drv')) + geom_point() \n", "p" ] }, { "cell_type": "markdown", "id": "d573a153-b030-442b-bafd-36331e32b5a3", "metadata": {}, "source": [ "#### 1. Customize the Legend' Labels\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "34a1cd8b-f202-454e-b60f-361ebd91ef08", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:55.305730Z", "iopub.status.busy": "2024-11-01T20:16:55.305655Z", "iopub.status.idle": "2024-11-01T20:16:55.310444Z", "shell.execute_reply": "2024-11-01T20:16:55.310275Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = p + scale_color_discrete(labels={\n", " 'f': 'front',\n", " 'r': 'rear',\n", " '4': '4WD' \n", "})\n", "p" ] }, { "cell_type": "markdown", "id": "brazilian-stack", "metadata": {}, "source": [ "#### 2. Customize the X-Axis `breaks` and `labels`" ] }, { "cell_type": "code", "execution_count": 6, "id": "amber-jackson", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:55.311513Z", "iopub.status.busy": "2024-11-01T20:16:55.311439Z", "iopub.status.idle": "2024-11-01T20:16:55.315704Z", "shell.execute_reply": "2024-11-01T20:16:55.315537Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = p + scale_x_continuous(breaks={\n", " 'min': 1.6,\n", " '3.4': 3.4,\n", " '5.2': 5.2,\n", " 'max': 7\n", "})\n", "p" ] }, { "cell_type": "markdown", "id": "classical-tournament", "metadata": {}, "source": [ "#### 3. Manually Reassign Colors " ] }, { "cell_type": "code", "execution_count": 7, "id": "departmental-inventory", "metadata": { "execution": { "iopub.execute_input": "2024-11-01T20:16:55.316753Z", "iopub.status.busy": "2024-11-01T20:16:55.316655Z", "iopub.status.idle": "2024-11-01T20:16:55.321318Z", "shell.execute_reply": "2024-11-01T20:16:55.321145Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_color_manual(values={ \n", " 'f': 'dark_blue', \n", " 'r': 'dark_green',\n", " '4': 'dark_magenta'\n", "})" ] } ], "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": 5 }