{ "cells": [ { "cell_type": "markdown", "id": "f7c40951-a35f-4921-b665-60b3c6de4d0c", "metadata": {}, "source": [ "# Using \"Exponent Format\"\n", "\n", "The `exponent_format` parameter in the `theme(...)` function can be used to configure the way \"exponent notation\" looks like on plot. \n", "Available values:\n", "- `'e'` for E notation, for example, 1.23e+3, which is the default format.\n", "- `'pow'` for superscript power notation.\n", "\n", "The \"exponent format\" is automatically applied to each value formatted in scientific notation, regardless whether the format is user-defined or chosen automatically based on the data. This format affects every part of a plot, including geoms, scales, labels, and tooltips.\n", "\n", "> #### Note:\n", ">\n", "> Do NOT(!) use `exponent_format='pow'` if you are planning to export plot to a raster format (PNG,PDF).\n", "> \n", "> The `CairoSVG` library (which is under the hood of our `ggsave()` function) does not handre `tspan` element properly end breaks superscript notation when transforming SVG to PNG/PDF.\n", ">\n", "> More details: https://github.com/Kozea/CairoSVG/issues/317" ] }, { "cell_type": "code", "execution_count": 1, "id": "7e5807d6-124c-461b-a9af-8f2cd88e0467", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.517665Z", "iopub.status.busy": "2024-08-23T10:38:12.517464Z", "iopub.status.idle": "2024-08-23T10:38:12.844380Z", "shell.execute_reply": "2024-08-23T10:38:12.843934Z" } }, "outputs": [], "source": [ "from lets_plot import *\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "id": "748b46fd-14e5-4636-a591-00c51658e83b", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.846278Z", "iopub.status.busy": "2024-08-23T10:38:12.846169Z", "iopub.status.idle": "2024-08-23T10:38:12.848416Z", "shell.execute_reply": "2024-08-23T10:38:12.848182Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "markdown", "id": "cbe482a2-a887-4fdf-be6c-3b6a8cab14f8", "metadata": {}, "source": [ "#### Set `exponent_format='pow'` for all plots in the notebook\n", "Each plot still can be cofigured individually: `p + theme(exponent_format='e'))`" ] }, { "cell_type": "code", "execution_count": 3, "id": "2eb75592-2610-438c-9d8a-d1abd78faea8", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.849975Z", "iopub.status.busy": "2024-08-23T10:38:12.849898Z", "iopub.status.idle": "2024-08-23T10:38:12.851337Z", "shell.execute_reply": "2024-08-23T10:38:12.851108Z" } }, "outputs": [], "source": [ "# LetsPlot.set_theme(theme(exponent_format='pow'))" ] }, { "cell_type": "code", "execution_count": 4, "id": "f119b573-4516-411b-9dbe-fbbf7a4a0c74", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.852645Z", "iopub.status.busy": "2024-08-23T10:38:12.852581Z", "iopub.status.idle": "2024-08-23T10:38:12.880413Z", "shell.execute_reply": "2024-08-23T10:38:12.879936Z" } }, "outputs": [], "source": [ "n = 10\n", "data = {\n", " 'x': list(range(n)),\n", " 'y': [(i + 1 + 0.025 * i) * 10**(-5) for i in range(n)],\n", " 'c': [i * 10**(10) for i in range(n)]\n", "}\n", "\n", "p = ggplot(data, mapping=aes(x='x', y='y', fill='c')) + geom_bar(stat='identity')" ] }, { "cell_type": "code", "execution_count": 5, "id": "4ea41bce-d220-4c57-bbf9-75112cb3852d", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.882160Z", "iopub.status.busy": "2024-08-23T10:38:12.882075Z", "iopub.status.idle": "2024-08-23T10:38:12.883761Z", "shell.execute_reply": "2024-08-23T10:38:12.883516Z" } }, "outputs": [], "source": [ "pow_theme = theme(exponent_format='pow')" ] }, { "cell_type": "markdown", "id": "ce1284d2-6d92-4540-b148-fcb411561d21", "metadata": {}, "source": [ "#### 1. E-notation vs. Superscript Power Notation\n", "\n", "In this example \"scientific notation\" formatting for the guides is chosen automatically, basing on the data." ] }, { "cell_type": "code", "execution_count": 6, "id": "5bc8222f-2d2e-4ea2-9305-f03c65949b73", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.885174Z", "iopub.status.busy": "2024-08-23T10:38:12.885102Z", "iopub.status.idle": "2024-08-23T10:38:12.891845Z", "shell.execute_reply": "2024-08-23T10:38:12.891605Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([p + ggtitle(\"E-notation (default)\"),\n", " p + pow_theme + ggtitle(\"Superscript Power\")])" ] }, { "cell_type": "markdown", "id": "a1a3148b-52ce-44ae-9885-81ebc994f013", "metadata": { "tags": [] }, "source": [ "#### 2. Formatting in `geom_text()` and `geom_label()`\n", "\n", "By default, values in `geom_text()` and `geom_label()` are always shown in standard notation. \n", "\n", "Thus by default, the `'pow'` in `theme()` doesn't affect numbers in `geom_text()` and `geom_label()` (see the chart on the left) unless
\n", "you specify a scientific notation formatting explicitly via the `label_format` parameter (see the chart on the right).\n" ] }, { "cell_type": "code", "execution_count": 7, "id": "78b1a24c-7240-4fd4-aaf9-609fb8cc7b59", "metadata": { "execution": { "iopub.execute_input": "2024-08-23T10:38:12.893255Z", "iopub.status.busy": "2024-08-23T10:38:12.893183Z", "iopub.status.idle": "2024-08-23T10:38:12.897098Z", "shell.execute_reply": "2024-08-23T10:38:12.896866Z" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "left = p + geom_label(aes(label='y'), alpha=0.8, fill='white')\n", "right = p + geom_label(aes(label='y'), alpha=0.8, fill='white', label_format='.2~e')\n", "\n", "gggrid([left, right]) + pow_theme" ] } ], "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 }