How to use this kind of scientific notation tick in plotly?

Viewed 664

I am using plotly with python and I am trying to use use these kinds of tick in X and Y axis: 10-3, 10-4, 10-5, and so on..

enter image description here

Any suggestions or recoomended reading?

I am obtaining this:

enter image description here

2 Answers

I believe the closes you'll get is logartihmic axes:

enter image description here

import plotly.express as px
df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country", log_x=True)
fig.show()

This has been bugging me for a while, here's the best solution I've found:

TLDR, you can use HTML formatting to make sure the exponent is a superscript and use array mode to pass strings for the tick labels to plotly.

import plotly.express as px
import numpy as np
df = px.data.gapminder().query("year == 2007")

fig = px.scatter(df, x="gdpPercap", y="lifeExp", hover_name="country", log_x=True)
tickvals = np.logspace(3,6, num=4)
print(tickvals)

def format_tick(n: float):
    m, e = f"{n:.0e}".split("e")
    e = int(e)
    return f"{m} x 10<sup>{e}</sup>"

ticktext = [format_tick(t) for t in tickvals]
print(ticktext)

fig.update_layout(
    xaxis = dict(
        tickmode = 'array',
        tickvals = tickvals,
        ticktext = ticktext,
    )
)
fig.show()
Related