Zhanhao Hu
Zhanhao Hu

Reputation: 53

Superscript exponents in log scale using Bokeh

I am using Bokeh to plot my research study data. I use the log scale a lot. But by default, the axis label of the log axis is shown like 10^2, instead of a superscript 2. The example plot from the Reference doc is exactly so: https://docs.bokeh.org/en/latest/docs/gallery/logaxis.html

I have checked answers to similar questions, and it seems one can use Latex to format the label (https://github.com/bokeh/bokeh/issues/6031). But the solution seems too complicated and it is hard to find out exactly how.

I wonder if there is a simple solution to this issue.

Thanks for any help.

Upvotes: 3

Views: 1365

Answers (2)

tcmetzger
tcmetzger

Reputation: 86

As of Bokeh 2.0, passing y_axis_type="log" to figure automatically displays exponents on log axes in a nice way:

enter image description here

For more complicated scenarios, Bokeh 2.4 adds support for LaTeX (and MathML) to some elements in Bokeh, including axis labels. You can now use plot.xaxis.axis_label = r"$$10^2$$", for example (using MathJax delimiters).

Currently, you can use LaTeX on axis labels, tick labels, div widgets, and paragraph widgets. LaTeX support for more elements should be added soon. For more information about the new math text feature and how to use them, see the Bokeh 2.4 release blogpost, the new blackbody radiation example, and the Bokeh user guide!

enter image description here

Upvotes: 0

Eugene Pakhomov
Eugene Pakhomov

Reputation: 10697

Note from maintainers: Initial built in LaTeX support was added in version 2.4, see this new answer https://stackoverflow.com/a/69198542/3406693


LaTeX can be used to add a label on top of the existing plot. Right now, it cannot be used for axes' titles.

However, the comment from the issue that you've linked attempts to solve it in a different way - by just using special superscript symbols.

Here's my attempt to make that solution shorter and easier to read:

p.yaxis[0].formatter = FuncTickFormatter(code="""
return 10 + (Math.log10(tick).toString()
             .split('')
             .map(function (d) { return d === '-' ? '⁻' : '⁰¹²³⁴⁵⁶⁷⁸⁹'[+d]; })
             .join(''));
""")

Upvotes: 2

Related Questions