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.
Note
Superscript is not supported when exporting to PNG/PDF.
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()
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()
def small_example(title, number_format=None):
dmin, dmax = -7, 6
values = [10**d for d in range(dmin, dmax + 1)]
return ggplot() + \
geom_text(aes(y=values, label=values), label_format=number_format, size=12) + \
scale_y_continuous(limits=[10**(dmin - 1), 10**(dmax + 1)], trans='log10', format=number_format) + \
ggtitle(title) + \
theme_classic()
def plot(number_format=None):
return 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)], \
format=number_format) + \
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))
plot()
gggrid([
small_example("Default format"),
small_example("format='g'", "g")
])
plot(number_format="g")
gggrid([
small_example("Default exponent_format", "g"),
small_example("exponent_format='pow'", "g") + theme(exponent_format='pow'),
small_example("exponent_format='pow_full'", "g") + theme(exponent_format='pow_full')
])
plot("g") + theme(exponent_format='pow')
gggrid([
small_example("Default limits", "g") + theme(exponent_format='pow'),
small_example("Scientific notation for \( x \leq 10^{-3} \) and \( x \geq 10^3 \)", "g") + \
theme(exponent_format=('pow', -3, 3))
])
plot("g") + theme(exponent_format=('pow', -14, None))