Reversing values on colorbar; not just colors


I’ve been trying to find a way to invert the values on a colorbar, making it so that the higher number is at the bottom of the bar, and the lower at the top. So far the only relevant thing I can find is the reversescale value under marker, but changing that to True or False just changes the orientation of the colors, but not the values. Is there a way to do this?


Hello @clairew,
In order to reverse the ticklabels on the colorbar, as well as the information to be displayed
on hover, we define a function that maps the interval [a,b], onto [b,a]: f(t, a, b)=b+a-t.

import plotly.graph_objects as go
import numpy as np

# function to reverse z-values to be displayed on hover, and on colorbar
f=lambda t, a, b :b+a-t 
z= np.array([[2, 3, 5, 7],
    [6, 2, 0, -1],
    [4, 3, 1, 6],
    [1, -2, 3, 4]])
a, b = z.min(), z.max()
fig=go.Figure(go.Heatmap(z=z, colorscale="matter", colorbar_thickness=24,
                         colorbar_tickvals=[-2, 1, 4, 7],
                         colorbar_ticktext=f([-2, 1, 4, 7], a, b),
                         customdata=f(z, a, b), hovertemplate='z: %{customdata}<extra></extra>'
              go.Layout(width=400, height=400, yaxis_scaleratio=1, yaxis_scaleanchor="x",
                       template="none", yaxis_zeroline=False, xaxis_zeroline=False))


1 Like