简体   繁体   中英

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

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..

在此处输入图像描述

Any suggestions or recoomended reading?

I am obtaining this:

在此处输入图像描述

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

在此处输入图像描述

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()

I was also in a search for this feature, and found the solution in this other answer:

https://stackoverflow.com/a/56742105/418875

You should set exponentformat to the power option. This will print the ticks as shown below:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM