Plotly is converting whole numbers to integers in Y axix ticks

Hi All,

I noticed that if the axis tick values are whole numbers- such as 1.0, 2.0, 3.0 etc., then plotly shows them as integers - 1, 2, 3 etc.

Here is a basic code sample to explain this issue -

import plotly.graph_objects as go

def tick_text():
    y = [1.0, 1.1, 1.23, 2.0, 2.1]
    x = [1, 2, 3, 4]

    fig = go.Figure(go.Scatter(x=x, y=y))
    fig.show()

if __name__ == "__main__":
    tick_text()

And here is the generate plot -

Is there a way to show the whole number y tick valus with 1 decimal place and other ticks with their default precision?

I am aware that I can use tickvals and ticktext like this -

fig.update_layout(
        yaxis=dict(
            tickmode='array',
            tickvals=y,
            ticktext=ticktext
        )
    )

But in this case, I have to provide my own Y axis values and I don’t want that. I want to be able to use scroll and zoom features of Y axis that comes by default when tickmode is set to β€˜auto’.

Is it possible to somehow stop this conversion of whole numbers to integers in Y axis ticks?

Any help would be highly appreciated.

Hi @sushmag ,

Welcome to the forum!

If you want to control decimal values that shows in y axis, you could use tickformat.

you can refer from link below:

To get all yaxis tick values with 1 decimal place, you can set tickformat to β€œ.1f”.

fig.update_yaxes(tickformat=".1f")

Hope this help.

2 Likes

Hi @farispriadi ,

Thank you for the welcome and reply!

I have tried using tickformat. But it sets the same precision for all values.

But since my values have different precision, my requirement is that -

y = [1.0, 1.1, 1.23, 2.0, 2.1]

1.0 is displayed as 1.0
1.1 is displayed as 1.1
1.23 is displayed as 1.23

If I set tickformat=".1f", then 1.23 is displayed as 1.2

Hi @sushmag,

As far as I know, customized precision on axis is not provided in official docs.

So maybe I can’t help you at this time.

I hope you find your best solution! :slightly_smiling_face:

Can you try fig.update_yaxes(type="category")