Black Lives Matter. Please consider donating to Black Girls Code today.
Dash HoloViews is now available! Check out the docs.

Need help formatting axis

I have created a plot with y values between 0 and 1e9. Plotly is automatically displaying the yaxis ticktext as numbers like [0, 0.1B, …, 0.8B, 0.9B, 1.0B], however, I would like the ticktext to have a precision of Millions, not Billions, ie [0, 100M, 200M, …, 800M, 900M, 1000M]. I know I can use both tickvals and ticktext to accomplish this, however, when I zoom in on a trace with values between, say, 120M and 130M, then there are no yaxis labels visible. I would like to use autorange, nticks, and set the precision of the y axis to be in the millions. Is there a way to do this?

Also, if this isn’t possible and we’re creating a wish list here, I would like to use autorange, nticks, and set the precision to be a maximum of Millions. For example, if a given trace was between 120e3 and 130e3, then the precision would be in K, or if the trace was between 120e6 and 130e6, then the precision would be in M, or if the trace was between 0 and 1e9, the precision would be in M.

Thanks for any help anyone can provide.

2 Likes

Hmm. I think you’re correct, there’s no way to make plotly do what you’re looking for.

I would be worth double-checking https://github.com/d3/d3-format/blob/master/README.md#api-reference (which plotly uses internally). I personally like http://bl.ocks.org/zanarmstrong/05c1e95bf7aa16c4768e for testing different formatting strings.

Thanks for writing in.

Thanks for the confirmation. I had looked at the d3.format() options, but it wasn’t quite what I was needing. I did see the locale.formatPrefix() function, which looks like it would be close to what I want, but I couldn’t see how to use that with plotly.

When looking at the code for plotly, I found the following file:

If there were an update to the numFormat() function to allow me to specify ax._tickexponent, perhaps with a new exponentFormat option, then I think this would be possible. Should I try to write that code and submit it somehow, or would someone here be better able to do all of that?