Using "Exponent Format"

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, for example, 1.23e+3, which is the default format.
  • 'pow' for superscript power notation.

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.

Note:

Do NOT(!) use exponent_format='pow' if you are planning to export plot to a raster format (PNG,PDF).

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.

More details: https://github.com/Kozea/CairoSVG/issues/317

In [1]:
from lets_plot import *
import numpy as np
In [2]:
LetsPlot.setup_html()

Set exponent_format='pow' for all plots in the notebook

Each plot still can be cofigured individually: p + theme(exponent_format='e'))

In [3]:
# LetsPlot.set_theme(theme(exponent_format='pow'))
In [4]:
n = 10
data = {
    'x': list(range(n)),
    'y': [(i + 1 + 0.025 * i) * 10**(-5) for i in range(n)],
    'c': [i * 10**(10) for i in range(n)]
}

p = ggplot(data, mapping=aes(x='x', y='y', fill='c')) + geom_bar(stat='identity')
In [5]:
pow_theme = theme(exponent_format='pow')

1. E-notation vs. Superscript Power Notation

In this example "scientific notation" formatting for the guides is chosen automatically, basing on the data.

In [6]:
gggrid([p + ggtitle("E-notation (default)"),
        p + pow_theme + ggtitle("Superscript Power")])
Out[6]:

2. Formatting in geom_text() and geom_label()

By default, values in geom_text() and geom_label() are always shown in standard notation.

Thus by default, the 'pow' in theme() doesn't affect numbers in geom_text() and geom_label() (see the chart on the left) unless
you specify a scientific notation formatting explicitly via the label_format parameter (see the chart on the right).

In [7]:
left  = p + geom_label(aes(label='y'), alpha=0.8, fill='white')
right = p + geom_label(aes(label='y'), alpha=0.8, fill='white', label_format='.2~e')

gggrid([left, right]) + pow_theme
Out[7]: