Logarithmic heatmap in Plotly

Viewed 1166

I am using heatmap from Plotly. I want to use a logarithmic scale for the color but cannot find how to do so. Here is a MWE:

import plotly.graph_objects as go
import numpy as np

z = [[1e-4,1e-3,1e-2],
    [1e-1, 1, 1e1],
    [1e2, 1e3, 1e4]]

go.Figure(
    data = go.Heatmap(
        z = z,
    )
).show()

go.Figure(
    data = go.Heatmap(
        z = np.log(z),
    )
).show()

In the MWE I manually calculate the logarithm of the data. I want the color map to be shown as in the second figure but without having to manually transform the data, and also displaying the real z values in the color scale, not the logarithm.

1 Answers

There is an example on the plotly homepage which I adapted to use the viridis colorscale. It's not perfect... you can play with the colorscale denominator to adapt it as you need.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np

viridis = px.colors.sequential.Viridis

z = [[1e-4,1e-3,1e-2],
    [1e-1, 1, 1e1],
    [1e2, 1e3, 1e4]]

fig1 = go.Figure(
    data = go.Heatmap(
        z = z,
    colorscale = [
        [0, viridis[0]],
        [1./1000000, viridis[2]],
        [1./10000, viridis[4]],
        [1./100, viridis[7]],
        [1., viridis[9]],
    ],
    colorbar= dict(
        tick0= 0,
        tickmode= 'array',
        tickvals= [0, 1000, 10000]
    )
    )
)

Another option would be to use:

colorscale = [
        [0, viridis[0]],
        [1./1000000, viridis[2]],
        [1./10000, viridis[4]],
        [1./100, viridis[7]],
        [1., viridis[9]],
]
px.imshow(z, color_continuous_scale=colorscale, height=600, width=600)

The first one gives you (the second option is rather similar...):

enter image description here

Related