Download notebook (.ipynb)

Working With Scientific Notation#

The exponent_format parameter in the theme(...) function can be used to configure the way “exponent notation” looks like on plot.

Available values:

  • 'e' for “e” notation (e.g. 1e+6);

  • 'pow_full' for “power” notation (e.g. $1 \cdot 10^6$). This will enable superscript formatting for the exponent;

  • 'pow' works as 'pow_full' but will shorten powers of 10 (e.g. $10^6$ instead of $1 \cdot 10^6$).

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.

import pandas as pd

from lets_plot import *
LetsPlot.setup_html()

Exponent Format Customization#

def hourglass_plot(title):
    dmin, dmax = -7, 6
    values = [10**d for d in range(dmin, dmax + 1)]
    return ggplot() + \
        geom_text(aes(y=values, label=values), size=10) + \
        scale_y_continuous(limits=[10**(dmin - 1), 10**(dmax + 1)], trans='log10') + \
        ggtitle(title) + \
        theme_classic()

Switch Exponent Format to Power Degree#

gggrid([
    hourglass_plot("exponent_format='e'") + theme(exponent_format='e'),
    hourglass_plot("exponent_format='pow' (default)") + theme(exponent_format='pow'),
    hourglass_plot("exponent_format='pow_full'") + theme(exponent_format='pow_full')
])

Specify the Limits From Which the Scientific Notation Used#

gggrid([
    hourglass_plot("Default limits") + theme(exponent_format='pow'),
    hourglass_plot("Scientific notation for \( x \leq 10^{-3} \) and \( x \geq 10^3 \)") + \
        theme(exponent_format=('pow', -3, 3))
])

Chemical Elements Demo#

amu_coeff_ng = 1.6605402 * 10**(-15)
df = pd.read_csv("https://github.com/JetBrains/lets-plot-docs/raw/refs/heads/master/data/chemical_elements.csv", \
                 encoding_errors='backslashreplace')
df = df[["Element", "Symbol", "Type", "Atomic Weight", "Atomic Radius"]]
df.columns = ["Name", "Symbol", "Type", "Weight (ng)", "Radius"]
df["Weight (ng)"] *= amu_coeff_ng
print(df.shape)
df.head()
(118, 5)
Name Symbol Type Weight (ng) Radius
0 Hydrogen H Nonmetal 1.673725e-15 0.79
1 Helium He Noble Gas 6.646482e-15 0.49
2 Lithium Li Alkali Metal 1.152581e-14 2.10
3 Beryllium Be Alkaline Earth Metal 1.496509e-14 1.40
4 Boron B Metalloid 1.795210e-14 1.20
ggplot(df) + \
    geom_label(aes("Weight (ng)", "Radius", label="Symbol", fill="Type"), \
               alpha=.75, fontface='bold') + \
    scale_x_continuous(limits=[0, 4*10**(-13)], \
                       breaks=[(i / 2.0)*10**(-13) for i in range(9)]) + \
    scale_fill_brewer(guide=guide_legend(ncol=2)) + \
    ggsize(1000, 500) + \
    theme(axis_text_x=element_text(size=16, face='bold'), \
          legend_position=(.91, .89), legend_key_size=12, legend_text=element_text(size=9))