Show numbers in billions with "B", like the axis, or alternatively control the number of digits

Hello, I am using plotly.express.bar, with text_auto=β€˜.3s’. to control the number of digits after the decimal point in the text. Unfortunately when the number is in the billions, the text suffix is β€˜G’, which doesn’t match the axis suffix β€˜B’. I understand this is as expected because β€˜s’ is using SI notation. However, is there another code I can use to get them to match, please? How does the axis labels do it?

When I use text_auto=True, the suffices match, but then the number of digits after the decimal is a lot more than I need. Perhaps there is an alternate way to control the number of digits, without resorting to the text_auto format string?

Thank you for your help.

Suffix mismatch: image , Too many digits: image

p.s. To be more precise, I would like to use the β€œB” notation, instead of β€œG”.

In this case, would it be possible to solve this problem by setting the same scale notation on the axis as well?

import plotly.express as px

df = px.data.gapminder().query("year == 2007 and pop > 2.e6")
df.sort_values('pop', ascending=False, inplace=True)

fig = px.bar(df[:10],
             y='pop',
             x='country',
             text=['{:.2}B'.format(x / 1000000000) for x in df['pop'][:10]],
             #text_auto='.2~s',
             title="Controlled text sizes, positions and angles")
fig.update_traces(textfont_size=12, textangle=0, textposition="outside", cliponaxis=False)
#fig.update_yaxes(tickformat="~s")
fig.show()

Thanks for that! I guess it’s the β€œG” notation that I don’t want to use, since my audience is more familiar with β€œB”. Should have been more precise, sorry. It’s good to know that I can control the tickformat, though.

p.s. I’ve since modified the subject line and problem description.

If you want to display it in billions, you could simply add a textual limit of digits in billions. Updated answer.

Thank you! That’s a great suggestion.

I ended up using a short β€œhuman_format” function found here β€œpython - Formatting long numbers as strings - Stack Overflow” to create my text. It is a little more flexible since it converts to other suffices automatically.

If you want to deal with this in a generic way, the link is as shown. I guess this question is closed. I need to respond to the termination.

Yes, thank you.