Custom tick labels in between ticks on heatmap

I have a heatmap with custom ticks (custom location and custom label). I would like the label to appear in between ticks. So far I managed to get this:

import numpy as np
import plotly.graph_objects as go
np.random.seed(13)

N = 100
M = 30
ticks = ['alpha', 'beta', 'gamma', 'zeta', 'theta', 'lambda']
ticks_locations = [0] + np.sort(np.random.randint(0, N, size=len(ticks)-1)).tolist()

image = np.random.randn(M, N)
fig = go.Figure(data=
    go.Heatmap(
        x=np.arange(N) + .5,
        z=image,
        colorscale='Bluered',
    )
)

fig.update_xaxes(
    tickvals=ticks_locations,
    ticktext=ticks,
    ticklen=20,
    tickcolor='black',
    ticks='outside',
    ticklabelposition='outside right',
    # tickson='boundaries',
)

fig.write_html('/tmp/tmp.html')

attempt so far

However, I would like my labels to appear in the middle in between the ticks (alpha between the first and the second tick, beta between the second and the third, and so on…).

Each label corresponds to a range of columns in the heatmap. When I zoom on the x-axis, I would like at least one tick to stay visible no matter the zoom level. More specifically, I would like to reproduce the behaviour of the outer labels (first and second) in multi-category axis: when you zoom on the x-axis, the outer label moves to stay visible on the axis.

I tried to play around with tickson='boundaries' but I was not able to get it to work.

2 Likes

@josephlegrand33

To move ticklabels to the right, you must change the values in tickvals, something like this:
tickvals=np.array(ticks_locations[:-1]) +a
where a is an array with a[i], the mid point between ticks_locations[i] and ticks_locations[i+1], i=0, 1, … len(ticks_locations)-1.

1 Like

This would also shift the tick right? I would like to only shift the label, and let the ticks where they are. Something like that:

|      alpha      |                beta                |   gamma   |      zeta      |...

@josephlegrand33 This isn’t possible.

1 Like

Thank you. It is unfortunate tho as it has been implemented for multi-categorcal axes

@josephlegrand33
For multicategory axes see here:Categorical axes in Python

Well that’s exactly the link I included in the original post