Skip to content
Advertisement

How to format a number from thousands to K style in plotly

I want to change the format of y-axis from thousands to thousands in K format in plotly python.

enter image description here

Advertisement

Answer

Since you didn’t provide any data, or even explain what you want to show exactly on the y-axis, I will answer with what should work:

fig.update_yaxes(tickformat=".2s")  # will format to 2 sigfigs

If you want regularly-spaced tick values for the y-axis, you can either pass a list of values, or use dtick and tick0, which are explained in the documentation

So for example, you might use

fig.update_yaxes(tickformat="~s", tickvals=[*range(100000, 1000000, 1000)])

For tick values in steps of 1000, starting at 100000, ending at 999000, trimming insignificant trailing zeros.

Note that the K suffix will switch to M once the tick values reach into the millions, and so on. If you always want to use K, you’ll need to define your own using the ticktext parameter:

tickvals = [*range(100_000, 1_000_000_000, 1000)]
ticktext = [f"{t // 1000:,}K" for t in tickvals]


fig.update_yaxes(tickvals=tickvals, ticktext=ticktext)

This will comma-separate and format numbers in thousands and append 'K', for example, 15485000 becomes '15,485K'.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement