Restrict axis ticks labels to only show int values

I have a plot with a y axis


It should only show integer values such as 1,2,3,4


Unfortunately when I zoom in floats are displayed (1.5,2.5,3.5)


Is there a way of telling plotly to only display ints


I tried using a list of predefined tickvals but this list is too long when zoomed out fully than the labels overlap


Thanks

2 Likes

You could try using tickformat like in http://codepen.io/etpinard/pen/OpyjjJ

This is nice but it produces multiple numbers which are the same.

β€œ1 1.5 2” turns into β€œ1 1 2”

Ideally I was looking for a method of turning β€œ1 1.5 2” into just β€œ1 2”

Thanks

There’s probably a way to do this. You can play around with all the possible format settings on http://bl.ocks.org/zanarmstrong/05c1e95bf7aa16c4768e

Thanks etienne your recommeded link worked very well

The format that removes rounded ints was d3.format(",d")

The graph looks much nicer now

2 Likes

In python, along:

fig = Figure(
    data = data,
    layout = Layout(
        barmode='stack',
        yaxis={'tickformat': ',d'},
4 Likes

This is an old but interesting point. However, the proposed solution is still giving me multiple numbers in a colorbar. I’m setting:

  colorbar = dict(title=f"{report['unit'][graph]}",
                                          outlinewidth=0.5,
                                          outlinecolor='black',
                                          len=0.7,
                                          thickness=0.02,
                                          thicknessmode='fraction',
                                          ticks='inside',
                                          tickformat=',d',
                                          yanchor='bottom',
                                          y=-0.025)

and the colorbar looks like:
Screenshot 2022-01-04 at 12.42.07

1 Like

Hello,

Were you guys able to solve this error? Even I am facing the same issues. for eg: Instead of 1, 1.5,2,2.5 converting to 1,2, it gets displayed as 1,1,2,2.

Also if I try to set the dtick as 1, it wont work for the plots which actually has large data.

Thanks
Mit

I’m having the same issue here. Is there really not a way to set a minimum axis interval?

EDIT: I just noticed that this post is marked with plotly JS. I think the proposed solution should work in JS also, that’s why I decided to not delete it :see_no_evil:

Hi,

you could fix the tickvals to integers:

import plotly.graph_objects as go
import numpy as np

def data():
    return np.random.uniform(5,10,1000)

x = data()
y = data()

fig = go.Figure(
        data=[
            go.Scatter(
                x=x, 
                y=y, 
                mode='markers',
                marker={
                    'color': y,
                    'colorscale': 'viridis', 
                    'colorbar': {'thickness': 10, 'tickformat': '.1f'},
                    #'colorbar': {'thickness': 10}
                },
                showlegend=False,
            )
        ]
)

fig.update_layout(
    xaxis={
        'range': [y.min(), y.max()], 
        'tickvals': [*range(int(y.min()), int(y.max())+2)]
    },
    width=500, 
    height=500
)

creates:
newplot(27)

1 Like