Use Plotly number formatter outside of Plotly


#1

I love Plotly’s automatic number formatter, that turns 1.40842e+09 into 1.40842B, 4.4853e+08 into 448.53M, etc.

My question is, how can I get that nice formatting outside of where it’s applied automatically (the axes)?

I’d like to use it in other parts, is there any D3 or Plotly easy workaround?

Thanks!


#2

Hi @xoelop,

The plotly number formatting is based on D3’s number formatting support. You can see the options at https://github.com/d3/d3-format. I believe the default behavior (where the SI suffix is displayed) is 's'.

Hope that helps!
-Jon


#3

I know, what I’m looking for is to use that not only where it’s enabled by plotly (in ‘tickformat’ property or axes), but in other parts of my python code (for example, to create the hover text, or to format any string out of plotly).

Is there any way to do this? Thanks Jon!


#4

Your link is great, looks like d3-format is modeled after Python 3’s format specification mini-language. But there’s only this thing that is missing, exactly what I’d like to have :sweat_smile:

To give an exact example, in JS with D3-format this would work:

d3.format(".2s")(42e6); would produce '42M

In Python , 's' is reserved for strings, so I haven’t found the way to do it.


#5

Hi @xoelop,

Unfortunately, I’m not aware of a way to do this directly in Python (without hand-coding something yourself)

-Jon


#6

Oh, what I suspected, what a pity. Thanks anyway @jmmease, have a nice day!