{ "cells": [ { "cell_type": "markdown", "id": "cd6ed382-76f2-4d90-ab8c-a09d18681822", "metadata": {}, "source": [ "# Working With Scientific Notation\n", "\n", "The `exponent_format` parameter in the `theme(...)` function can be used to configure the way \"exponent notation\" looks like on plot.\n", "\n", "Available values:\n", "\n", "- `'e'` for \"e\" notation (e.g. 1e+6);\n", "- `'pow_full'` for \"power\" notation (e.g. $1 \\cdot 10^6$). This will enable superscript formatting for the exponent;\n", "- `'pow'` works as `'pow_full'` but will shorten powers of 10 (e.g. $10^6$ instead of $1 \\cdot 10^6$).\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", "> Superscript is not supported when exporting to PNG/PDF." ] }, { "cell_type": "code", "execution_count": 1, "id": "440ed621-4a10-4f0a-8f39-36f05afb56b5", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "from lets_plot import *" ] }, { "cell_type": "code", "execution_count": 2, "id": "f4564dc9-f210-4d74-8f7c-04eacaca2b4d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 3, "id": "b9028a79-4879-4537-a0e0-a5d78aa07de6", "metadata": {}, "outputs": [], "source": [ "amu_coeff_ng = 1.6605402 * 10**(-15)" ] }, { "cell_type": "code", "execution_count": 4, "id": "52633b78-510f-4769-9a97-e297d43fbb66", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(118, 5)\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", "
NameSymbolTypeWeight (ng)Radius
0HydrogenHNonmetal1.673725e-150.79
1HeliumHeNoble Gas6.646482e-150.49
2LithiumLiAlkali Metal1.152581e-142.10
3BerylliumBeAlkaline Earth Metal1.496509e-141.40
4BoronBMetalloid1.795210e-141.20
\n", "
" ], "text/plain": [ " Name Symbol Type Weight (ng) Radius\n", "0 Hydrogen H Nonmetal 1.673725e-15 0.79\n", "1 Helium He Noble Gas 6.646482e-15 0.49\n", "2 Lithium Li Alkali Metal 1.152581e-14 2.10\n", "3 Beryllium Be Alkaline Earth Metal 1.496509e-14 1.40\n", "4 Boron B Metalloid 1.795210e-14 1.20" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"https://github.com/JetBrains/lets-plot-docs/raw/refs/heads/master/data/chemical_elements.csv\", \\\n", " encoding_errors='backslashreplace')\n", "df = df[[\"Element\", \"Symbol\", \"Type\", \"Atomic Weight\", \"Atomic Radius\"]]\n", "df.columns = [\"Name\", \"Symbol\", \"Type\", \"Weight (ng)\", \"Radius\"]\n", "df[\"Weight (ng)\"] *= amu_coeff_ng\n", "print(df.shape)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 5, "id": "e43d5ec9-c65b-4b2e-a143-8d718611c3d5", "metadata": {}, "outputs": [], "source": [ "def small_example(title, number_format=None):\n", " dmin, dmax = -7, 6\n", " values = [10**d for d in range(dmin, dmax + 1)]\n", " return ggplot() + \\\n", " geom_text(aes(y=values, label=values), label_format=number_format, size=12) + \\\n", " scale_y_continuous(limits=[10**(dmin - 1), 10**(dmax + 1)], trans='log10', format=number_format) + \\\n", " ggtitle(title) + \\\n", " theme_classic()" ] }, { "cell_type": "code", "execution_count": 6, "id": "649a2046-a630-4bdf-89b5-2db5a328dfc3", "metadata": {}, "outputs": [], "source": [ "def plot(number_format=None):\n", " return ggplot(df) + \\\n", " geom_label(aes(\"Weight (ng)\", \"Radius\", label=\"Symbol\", fill=\"Type\"), alpha=.75, fontface='bold') + \\\n", " scale_x_continuous(limits=[0, 4*10**(-13)], \\\n", " breaks=[(i / 2.0)*10**(-13) for i in range(9)], \\\n", " format=number_format) + \\\n", " scale_fill_brewer(guide=guide_legend(ncol=2)) + \\\n", " ggsize(1000, 500) + \\\n", " theme(axis_text_x=element_text(size=16, face='bold'), \\\n", " legend_position=(.91, .89), legend_key_size=12, legend_text=element_text(size=9))" ] }, { "cell_type": "markdown", "id": "417d900a-0543-4ad8-b26c-a31d090854cf", "metadata": {}, "source": [ "### Default Plot" ] }, { "cell_type": "code", "execution_count": 7, "id": "21721a85-41d8-49e1-b04f-7ada592e3683", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot()" ] }, { "cell_type": "markdown", "id": "19d90cd6-fd93-4ea3-a43c-f95c72615c43", "metadata": {}, "source": [ "### Specify Format" ] }, { "cell_type": "code", "execution_count": 8, "id": "b5245bf7-61c2-4587-9be8-6c228f649007", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " small_example(\"Default format\"),\n", " small_example(\"format='g'\", \"g\")\n", "])" ] }, { "cell_type": "code", "execution_count": 9, "id": "b35ec8bc-7d5a-474e-884a-f643f60ac1c0", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(number_format=\"g\")" ] }, { "cell_type": "markdown", "id": "a5b79f37-2693-4f2f-bc0f-a8b5bd44c08f", "metadata": {}, "source": [ "### Switch Exponent Format to Power Degree" ] }, { "cell_type": "code", "execution_count": 10, "id": "cf098257-6060-4d80-b498-2b7c660163b1", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " small_example(\"Default exponent_format\", \"g\"),\n", " small_example(\"exponent_format='pow'\", \"g\") + theme(exponent_format='pow'),\n", " small_example(\"exponent_format='pow_full'\", \"g\") + theme(exponent_format='pow_full')\n", "])" ] }, { "cell_type": "code", "execution_count": 11, "id": "9837d45a-63ca-4722-90d6-e029c1f7444a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(\"g\") + theme(exponent_format='pow')" ] }, { "cell_type": "markdown", "id": "3db5b2e0-830b-49dc-84d6-df69e5f87ff1", "metadata": {}, "source": [ "### Specify the Limits From Which the Scientific Notation Used" ] }, { "cell_type": "code", "execution_count": 12, "id": "007f162f-f337-4460-a6a4-f232fe7bc5af", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "gggrid([\n", " small_example(\"Default limits\", \"g\") + theme(exponent_format='pow'),\n", " small_example(\"Scientific notation for \\( x \\leq 10^{-3} \\) and \\( x \\geq 10^3 \\)\", \"g\") + \\\n", " theme(exponent_format=('pow', -3, 3))\n", "])" ] }, { "cell_type": "code", "execution_count": 13, "id": "6efcf863-4602-4cfb-ab56-780ff982f4dc", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " " ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "plot(\"g\") + theme(exponent_format=('pow', -14, None))" ] } ], "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.9.20" } }, "nbformat": 4, "nbformat_minor": 5 }