{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatting labels on plots.\n", "\n", "In Lets-Plot you can apply a formatting to:\n", "\n", "- axis break values.\n", "- labels displayed by `geom_text()`.\n", "- tooltip text.\n", "- facet labels.\n", "\n", "Using format string you can format values of numeric and date-time types.\n", "\n", "In addition, you can use a *string template*.\n", "\n", "In *string template* the value's format string is surrounded by curly braces: `\"... {.2f} ...\"`.\n", "\n", "An empty placeholder `{}` is also allowed. In this case a default string representation will be shown. This is also applicable to categorical values.\n", "\n", "See [Formatting](https://lets-plot.org/python/pages/formats.html) documentation page to find information about supported format strings and string templates." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:55.999887Z", "iopub.status.busy": "2024-04-26T11:45:55.999887Z", "iopub.status.idle": "2024-04-26T11:45:56.945240Z", "shell.execute_reply": "2024-04-26T11:45:56.944496Z" } }, "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": "2024-04-26T11:45:56.945240Z", "iopub.status.busy": "2024-04-26T11:45:56.945240Z", "iopub.status.idle": "2024-04-26T11:45:56.960339Z", "shell.execute_reply": "2024-04-26T11:45:56.960339Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The US Unemployment Rates 2000-2016" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:56.978867Z", "iopub.status.busy": "2024-04-26T11:45:56.978867Z", "iopub.status.idle": "2024-04-26T11:45:57.151412Z", "shell.execute_reply": "2024-04-26T11:45:57.150724Z" } }, "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", "
Unnamed: 0datepcepoppsavertuempmedunemploy
011967-07-01506.7198712.012.64.52944
121967-08-01509.8198911.012.64.72945
231967-09-01515.6199113.011.94.62958
\n", "
" ], "text/plain": [ " Unnamed: 0 date pce pop psavert uempmed unemploy\n", "0 1 1967-07-01 506.7 198712.0 12.6 4.5 2944\n", "1 2 1967-08-01 509.8 198911.0 12.6 4.7 2945\n", "2 3 1967-09-01 515.6 199113.0 11.9 4.6 2958" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "economics_url = 'https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv'\n", "economics = pd.read_csv(economics_url, parse_dates=['date'])\n", "start_date = economics['date'].min()\n", "economics.head(3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default plot (no formatting)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.151412Z", "iopub.status.busy": "2024-04-26T11:45:57.151412Z", "iopub.status.idle": "2024-04-26T11:45:57.262097Z", "shell.execute_reply": "2024-04-26T11:45:57.261388Z" } }, "outputs": [], "source": [ "p = (ggplot(economics, aes('date', 'uempmed')) +\n", " geom_line() +\n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400)\n", " )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.262097Z", "iopub.status.busy": "2024-04-26T11:45:57.262097Z", "iopub.status.idle": "2024-04-26T11:45:57.293266Z", "shell.execute_reply": "2024-04-26T11:45:57.293266Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p + scale_x_datetime()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Apply formatting to X and Y axis labels.\n", "\n", "Use the `format` parameter in `scale_xxx()`.\n", "\n", "Note that the text in tooltips is now also formatted." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.293266Z", "iopub.status.busy": "2024-04-26T11:45:57.293266Z", "iopub.status.idle": "2024-04-26T11:45:57.324700Z", "shell.execute_reply": "2024-04-26T11:45:57.324700Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(p + \n", " scale_x_datetime(format=\"%b %Y\") + \n", " scale_y_continuous(format=\"{} %\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Format axis labels for breaks specified manually." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.324700Z", "iopub.status.busy": "2024-04-26T11:45:57.324700Z", "iopub.status.idle": "2024-04-26T11:45:57.356857Z", "shell.execute_reply": "2024-04-26T11:45:57.355947Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "breaks = pd.date_range(\n", " pd.to_datetime(\"2001-01-01\"), \n", " pd.to_datetime(\"2016-01-01\"), \n", " freq='5YS'\n", ").to_pydatetime()\n", "\n", "(p + \n", " scale_x_datetime(format=\"%b %Y\", breaks=breaks) + \n", " scale_y_continuous(format=\"{} %\")\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure tooltip's text and location." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.356857Z", "iopub.status.busy": "2024-04-26T11:45:57.356857Z", "iopub.status.idle": "2024-04-26T11:45:57.387871Z", "shell.execute_reply": "2024-04-26T11:45:57.387871Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot(economics, aes('date', 'uempmed')) + \n", " geom_line(tooltips=layer_tooltips()\n", " .line(\"Unemployment rate:|^y\")\n", " .anchor(\"top_center\")\n", " .min_width(170)) + \n", " scale_x_datetime(format=\"%b %Y\") + \n", " scale_y_continuous(format=\"{} %\") + \n", " ylab(\"unemployment rate\") +\n", " ggsize(900, 400))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Format value shown in tooltip." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.387871Z", "iopub.status.busy": "2024-04-26T11:45:57.387871Z", "iopub.status.idle": "2024-04-26T11:45:57.419161Z", "shell.execute_reply": "2024-04-26T11:45:57.419161Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(ggplot(economics, aes('date', 'uempmed')) + \n", " ylab(\"unemployment rate\") +\n", " scale_x_datetime() +\n", " scale_y_continuous() +\n", " geom_line(tooltips=layer_tooltips()\n", " .line('@uempmed % in @date')\n", " .format('date', '%B %Y')\n", " .anchor(\"top_left\")\n", " .min_width(170)) +\n", " ggsize(900, 400)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Add the unemployment rate mean.\n", "\n", "The `geom_text` label is formatted using the `label_format` parameter." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2024-04-26T11:45:57.419161Z", "iopub.status.busy": "2024-04-26T11:45:57.419161Z", "iopub.status.idle": "2024-04-26T11:45:57.450536Z", "shell.execute_reply": "2024-04-26T11:45:57.450536Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemployment_mean = economics[\"uempmed\"].mean()\n", "\n", "(ggplot(economics, aes('date', 'uempmed')) + \n", " geom_line(tooltips=layer_tooltips()\n", " .line(\"Unemployment rate:|^y\")\n", " .anchor(\"top_center\")\n", " .min_width(170)) + \n", " geom_hline(yintercept=unemployment_mean, color=\"red\", linetype=\"dashed\") +\n", " geom_text(label=unemployment_mean, \n", " label_format=\"{.2f} %\",\n", " x=start_date, y=unemployment_mean+.5, \n", " color=\"red\") +\n", " scale_x_datetime(format=\"%b %Y\") + \n", " scale_y_continuous(format=\"{} %\") + \n", " ylab(\"unemployment rate\") +\n", " ggtitle(\"The US Unemployment Rates 2000-2016.\") +\n", " ggsize(900, 400))" ] } ], "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 }