Adjusting space between ticks

I originally had a problem similar to this thread where the y-axis ticks would be very squished together. The suggestion was to set a pre-determined height.

However, my graph is of variable height. There could be anywhere from one to hundreds of points.

I start off with fig = go.Figure() and then add traces. Afterwards, I set the layout through something like this:

fig.update_layout(
        height=num_yaxis_ticks * 25 + 250, showlegend=False,
        yaxis=dict(tickmode='linear'), 
        xaxis=dict(side='top')
    )

I need all the yaxis ticks to be shown.

This works okay for most of the time, but the more y_axis ticks, the greater the space at the top and bottom of the graph. Here are some pictures for reference (yaxis tick labels cutoff for anonymity).

Here is a pretty normal looking graph with 4 yaxis ticks:

Here is a troublesome graph with at least 100 yaxis ticks (the bottom is cropped off) and a huge space at the top and bottom:

Any help is appreciated!

I had a similar problem with labeled ticks, i’m not sure if this will work for you.
What i did is to set the tick0 to 0, dtick to 1 and add a range.

fig.update_yaxes(
    autorage=False,
    range = [-1,len(df)],
    tick0=0,
    dtick=1
)
1 Like

Thank you for solving a problem I have been banging my head against on and off for over a year. I originally asked the
question here:

How to fix ugly tick spacing on line chart with small data set.

And never got a satisfactory answer- I tried randomly searching again tonight and came across your post, which finally solved my problem!

This code (where data[β€œYear”] is a pd.Series of years in format YYYY:

fig.update_layout(
    xaxis=dict(
        tickvals=data["Year"],
        autorange=False,
        range=[0, len(data["Year"]) - 1],
        tick0=0,
        dtick=1
    ),
)

Gave me exactly what I was looking for, evenly spaced x-axis ticks starting at the left edge and ending at the right edge.

Thank you!