{ "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": "2026-01-27T17:09:34.322042Z", "iopub.status.busy": "2026-01-27T17:09:34.321978Z", "iopub.status.idle": "2026-01-27T17:09:34.324817Z", "shell.execute_reply": "2026-01-27T17:09:34.324492Z" } }, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:34.325612Z", "iopub.status.busy": "2026-01-27T17:09:34.325542Z", "iopub.status.idle": "2026-01-27T17:09:34.327603Z", "shell.execute_reply": "2026-01-27T17:09:34.327055Z" } }, "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": "2026-01-27T17:09:34.340401Z", "iopub.status.busy": "2026-01-27T17:09:34.340311Z", "iopub.status.idle": "2026-01-27T17:09:34.442910Z", "shell.execute_reply": "2026-01-27T17:09:34.442537Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(574, 7)\n" ] }, { "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: 0datepcepoppsavertuempmedunemploy
011967-07-01506.7198712.012.64.52944
121967-08-01509.8198911.012.64.72945
231967-09-01515.6199113.011.94.62958
341967-10-01512.2199311.012.94.93143
451967-11-01517.4199498.012.84.73066
\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\n", "3 4 1967-10-01 512.2 199311.0 12.9 4.9 3143\n", "4 5 1967-11-01 517.4 199498.0 12.8 4.7 3066" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv\", parse_dates=[\"date\"])\n", "start_date = df[\"date\"].min()\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Default plot (no formatting)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:34.443833Z", "iopub.status.busy": "2026-01-27T17:09:34.443762Z", "iopub.status.idle": "2026-01-27T17:09:34.469288Z", "shell.execute_reply": "2026-01-27T17:09:34.468988Z" } }, "outputs": [], "source": [ "p = ggplot(df, aes(\"date\", \"uempmed\")) + \\\n", " geom_line() + \\\n", " ylab(\"unemployment rate\") + \\\n", " ggsize(900, 400)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:34.470258Z", "iopub.status.busy": "2026-01-27T17:09:34.470180Z", "iopub.status.idle": "2026-01-27T17:09:34.479500Z", "shell.execute_reply": "2026-01-27T17:09:34.479270Z" } }, "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": "2026-01-27T17:09:34.480433Z", "iopub.status.busy": "2026-01-27T17:09:34.480359Z", "iopub.status.idle": "2026-01-27T17:09:34.486485Z", "shell.execute_reply": "2026-01-27T17:09:34.486120Z" } }, "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": "2026-01-27T17:09:34.487219Z", "iopub.status.busy": "2026-01-27T17:09:34.487130Z", "iopub.status.idle": "2026-01-27T17:09:34.502790Z", "shell.execute_reply": "2026-01-27T17:09:34.502506Z" } }, "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=\"{} %\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Configure tooltip's text and location" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2026-01-27T17:09:34.503774Z", "iopub.status.busy": "2026-01-27T17:09:34.503690Z", "iopub.status.idle": "2026-01-27T17:09:34.534373Z", "shell.execute_reply": "2026-01-27T17:09:34.534107Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, 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": "2026-01-27T17:09:34.535331Z", "iopub.status.busy": "2026-01-27T17:09:34.535241Z", "iopub.status.idle": "2026-01-27T17:09:34.542271Z", "shell.execute_reply": "2026-01-27T17:09:34.542020Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, 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)" ] }, { "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": "2026-01-27T17:09:34.543311Z", "iopub.status.busy": "2026-01-27T17:09:34.543222Z", "iopub.status.idle": "2026-01-27T17:09:34.551420Z", "shell.execute_reply": "2026-01-27T17:09:34.551124Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unemployment_mean = df[\"uempmed\"].mean()\n", "\n", "ggplot(df, 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,\n", " color=\"red\",\n", " linetype='dashed',\n", " tooltips='none') + \\\n", " geom_text(label=unemployment_mean, \n", " label_format=\"{.2f} %\",\n", " x=start_date,\n", " 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 }